Page 1 of 1
set command behavior and auto creating path
Posted: 03 Feb 2008 09:47
by Macarong
Hi,
I do not know why the set command inside () won't show outside of its (). My test code is below:
Code: Select all
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
:loop
set fileLocate=
set /p fileLocate= Enter the file name:
if "%fileLocate%" EQU "" goto loop
if /i "%fileLocate:~-4%" EQU ".ext" (
echo file with ext
) ELSE (
echo Do not have ext and add it
set fileLocate = %fileLocate%.ext
)
echo %fileLocate%
Btw, does anyone has a code to create a path in case of the path input does not exist?
ps: I run this in cmd of Vista
ver= Microsoft Windows [Version 6.0.6000]
some examples here generated errors too.
Any adjustment needed in Vista cmd?
Thanks in advance,
Posted: 04 Feb 2008 22:10
by DosItHelp
Macarong,
Your problem is an extra space, the following two lines are not the same:
Code: Select all
set fileLocate = %fileLocate%.ext
set fileLocate= %fileLocate%.ext
DOS IT HELP?
Posted: 06 Feb 2008 05:50
by jeb
Hi Macarong,
Btw, does anyone has a code to create a path in case of the path input does not exist?
I suppose you want to create a path "c:\temp\myPath\mySrc" if it is not exists.
you can simple use
if the path, it will result with an error, but that can be ignored.
Jan Erik
Posted: 09 Feb 2008 06:38
by Macarong
DoesItHelp,
DosItHelp wrote:Your problem is an extra space, the following two lines are not the same:
Code: Select all
set fileLocate = %fileLocate%.ext
set fileLocate= %fileLocate%.ext
DOS IT HELP?
Yes, it helps.
Thanks,
McR
Posted: 09 Feb 2008 07:00
by Macarong
jeb wrote:Hi Macarong,
Btw, does anyone has a code to create a path in case of the path input does not exist?
I suppose you want to create a path "c:\temp\myPath\mySrc" if it is not exists.
you can simple use
if the path, it will result with an error, but that can be ignored.
Jan Erik
Thank Jeb,
This works. I can use error redirection to a file then check its size to catch the error in case it cannot create the pah.
My new question is
+++++++++++++++++++++
If a user enters c:\tmp\tmp1\tmp2\file.txt or
c:\tmp\tmp1 add\file 22.exxt
I means an abitrary folder length and/or filename and/or space.
How can I take out a seperate folder and a filename parts?
In the above examples, I want to have:
echo %a% will result as "c:\tmp\tmp1\tmp2"
echo %b% will result as file.txt
OR
echo %c% will result as "c:\tmp\tmp1 add"
echo %d% will result as "file 22.exxt"
many Thanks
McR
Split path
Posted: 12 Feb 2008 02:51
by jeb
Hi,
reference at FOR /?
splitPath.bat
Code: Select all
echo Path=%~dp1
echo filename=%~nx1
Use it like
Code: Select all
splitPath "c:\tmp\tmp1 add\file 22.exxt"
Jan Erik
Re: Split path
Posted: 12 Feb 2008 10:07
by Macarong
jeb wrote:Hi,
reference at FOR /?
splitPath.bat
Code: Select all
echo Path=%~dp1
echo filename=%~nx1
Use it like
Code: Select all
splitPath "c:\tmp\tmp1 add\file 22.exxt"
Jan Erik
many thanks Jeb,
It looks simple but works perfectly.
You must be top of the best of DOS gurus.
I am just a new learner but loves script.
Can you introduce me where can I find a similar help on vbscript, wsf, and hta?
If I want to write a portable script that can run in both windows and Unix/Linux, what language should I learn? Javascript?
best wishes,
McR