Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Jer
- Posts: 177
- Joined: 23 Nov 2014 17:13
- Location: California USA
#1
Post
by Jer » 02 Jun 2015 20:40
This batch file runs okay in c:\Temp.
When I move it to C:\Test Folder, messages say
the system cannot find the path specified.
and the batch file continues but does not do what is expected, sorting a list in random order.
I know it is because of the space. How do I fix it?
Thanks.
Code: Select all
@Echo Off
setlocal enabledelayedexpansion
::random sorting
Set thisPath=%~dp0
Set creaturesList=%thisPath%original.txt
Set rsortedList=%thisPath%rsorted.txt
If EXIST %creaturesList% DEL %creaturesList%
For %%a In (Aardvark Beetle Cat Dog Emu Frog Gopher Hamster Iguana) Do (Echo %%a>>%creaturesList%)
Call:sortRandom %creaturesList% %rsortedList%
Echo.& Echo original list:& Echo.
For /F %%a In (%creaturesList%) Do (
Echo %%a
)
Echo.& Echo random sorted:
Echo.& Type %rsortedList%& Echo.
exit /b
:sortRandom
setlocal enabledelayedexpansion
Type NUL>tmp.txt
For /F %%a In (%~1) Do (
Set rndVar=!RANDOM!
Set rndVar=0000!rndVar!
Set rndVar=!rndVar:~-5!
Echo !rndVar! %%a>>tmp.txt
)
SORT tmp.txt>sorted.txt
Type NUL>%2
For /F "tokens=1,2* delims= " %%a In (sorted.txt) Do (
Echo %%b>>%2
)
DEL sorted.txt
DEL tmp.txt
endlocal & exit /b
endlocal
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#2
Post
by penpen » 03 Jun 2015 02:08
You could use doublequotes around your filenames.
If you use "for /F" then you have to use the option "usebackq" else the file within doublequotes is interpreted as a string.
Untested (changed on mobile phone):
Code: Select all
@Echo Off
setlocal enabledelayedexpansion
::random sorting
Set thisPath=%~dp0
Set creaturesList=%thisPath%original.txt
Set rsortedList=%thisPath%rsorted.txt
If EXIST "%creaturesList%" DEL "%creaturesList%"
For %%a In (Aardvark Beetle Cat Dog Emu Frog Gopher Hamster Iguana) Do (Echo %%a>>"%creaturesList%")
Call:sortRandom "%creaturesList%" "%rsortedList%"
Echo.& Echo original list:& Echo.
For /F "usebackq" %%a In ("%creaturesList%") Do (
Echo %%a
)
Echo.& Echo random sorted:
Echo.& Type "%rsortedList%"& Echo.
exit /b
:sortRandom
setlocal enabledelayedexpansion
Type NUL>tmp.txt
For /F "usebackq" %%a In ("%~1") Do (
Set rndVar=!RANDOM!
Set rndVar=0000!rndVar!
Set rndVar=!rndVar:~-5!
Echo !rndVar! %%a>>tmp.txt
)
SORT tmp.txt>sorted.txt
Type NUL>%2
For /F "tokens=1,2* delims= " %%a In (sorted.txt) Do (
Echo %%b>>%2
)
DEL sorted.txt
DEL tmp.txt
endlocal & exit /b
penpen
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#3
Post
by foxidrive » 03 Jun 2015 02:47
Things like these need quoting too.
Code: Select all
@Echo Off
setlocal enabledelayedexpansion
::random sorting
Set "thisPath=%~dp0"
Set "creaturesList=%thisPath%original.txt"
Set "rsortedList=%thisPath%rsorted.txt"
If EXIST "%creaturesList%" DEL "%creaturesList%"
-
Jer
- Posts: 177
- Joined: 23 Nov 2014 17:13
- Location: California USA
#4
Post
by Jer » 03 Jun 2015 09:42
Your showing how it is done and explanations of using quotes and usebackq are
10x better than /?
Thank you both. The code runs without error when a folder name includes spaces.