Page 1 of 1

Referencing file path-need help with spaces

Posted: 02 Jun 2015 20:40
by Jer
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

Re: Referencing file path-need help with spaces

Posted: 03 Jun 2015 02:08
by penpen
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

Re: Referencing file path-need help with spaces

Posted: 03 Jun 2015 02:47
by foxidrive
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%"


Re: Referencing file path-need help with spaces

Posted: 03 Jun 2015 09:42
by Jer
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.