set command behavior and auto creating path

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Macarong
Posts: 4
Joined: 23 Oct 2007 07:41

set command behavior and auto creating path

#1 Post by Macarong » 03 Feb 2008 09:47

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? :roll:

Thanks in advance,

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 04 Feb 2008 22:10

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? :wink:

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#3 Post by jeb » 06 Feb 2008 05:50

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

Code: Select all

md \temp\myPath\mySrc

if the path, it will result with an error, but that can be ignored.


Jan Erik

Macarong
Posts: 4
Joined: 23 Oct 2007 07:41

#4 Post by Macarong » 09 Feb 2008 06:38

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? :wink:


Yes, it helps.
Thanks,
McR :D

Macarong
Posts: 4
Joined: 23 Oct 2007 07:41

#5 Post by Macarong » 09 Feb 2008 07:00

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

Code: Select all

md \temp\myPath\mySrc

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 :roll:

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Split path

#6 Post by jeb » 12 Feb 2008 02:51

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

Macarong
Posts: 4
Joined: 23 Oct 2007 07:41

Re: Split path

#7 Post by Macarong » 12 Feb 2008 10:07

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. :D
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

Post Reply