inGearX3.1415 9265 join:2000-06-11 New York 1 edit |
this BAT fails if there are spaces in files - easy fix?I have a BAT that I run it goes to folder and findes that last modified file and it gets the name and opens it (and c:\z\ClipCopy.exe can put the path into the clipboard) well my Video Player when capturing screen shots - files have spaces :/ and this upsets the script :/ any way to fix this? thanks .. REM @echo off
cd /d C:\y\_captures
dir /od /b > %temp%\latest.txt
for /F %%a in (%temp%\latest.txt) do set LATEST_FILE=%%a
print %LATEST_FILE%
c:\z\ClipCopy.exe "C:\y\_captures\%LATEST_FILE%"
C:\y\_captures\%LATEST_FILE%
REM pause
|
|
inGearX |
for now I just added a hack that renames ALL files to remove spaces REM @echo off
cd /d C:\y\_captures
setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
REM set filename=!filename:.=_!
set filename=!filename: =_!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
)
dir /od /b > %temp%\latest.txt
for /F %%a in (%temp%\latest.txt) do set LATEST_FILE=%%a
print %LATEST_FILE%
c:\z\ClipCopy.exe "C:\y\_captures\%LATEST_FILE%"
C:\y\_captures\%LATEST_FILE%
REM pause
|
|
|
to inGearX
Use "around file or path . names" |
|
|
to inGearX
You can try putting quotes around the variable
for /F %%a in (%temp%\latest.txt) do set "LATEST_FILE"=%%a
that way it should get the full file name spaces and all, other wise it stops at the first space. |
|
dave Premium Member join:2000-05-04 not in ohio |
dave
Premium Member
2015-Feb-27 7:18 am
You have that backwards. the word LATEST_FILE contains no spaces, therefore there is no need to quote it.
Instead, you want to add quotes where the variable is expanded in contexts that are space-sensitive. For example,
"C:\y\_captures\%LATEST_FILE%"
Oddly enough, the OP used the correct approach in his rename-to-remove-spaces script, but did not see that he could have more simply applied the same fix in the original code. |
|
|
to inGearX
Yes I see he had the Copy line quoted so i assumed that the assignment need to be quoted. |
|
|
19579823 (banned)An Awesome Dude join:2003-08-04 |
to inGearX
See if you can rename your file using the ~ symbol..... On my computer if a file has a space or is over a certain amount of characters,the file gets renamed... For example .. "1 2".exe gets named "12~1".exe (If ya look @ properties and all for the file) -- I would think you may be able to use ~ in your script,im not sure..... Good luck |
|
dave Premium Member join:2000-05-04 not in ohio |
dave
Premium Member
2015-Mar-3 12:15 pm
The command language has ways to handle this, so all he has to do is to *use* the ways that the command language provides to handle this. The correct, simplest, and easiest solution was given in the first reply by aurgathor . |
|
DarkLogixTexan and Proud Premium Member join:2008-10-23 Baytown, TX |
to mmainprize
Re: this BAT fails if there are spaces in files - easy fix?said by mmainprize:You can try putting quotes around the variable Might have to do ""double quotes"" or """Triple""" as sometimes its needed to put the quote marks in quotes. |
|
dave Premium Member join:2000-05-04 not in ohio |
dave
Premium Member
2015-Mar-3 3:08 pm
Not in this case you don't. The solution is trivial. REM @echo off
cd /d C:\y\_captures
dir /od /b > %temp%\latest.txt
for /F %%a in (%temp%\latest.txt) do set LATEST_FILE=%%a
print "%LATEST_FILE%"
c:\z\ClipCopy.exe "C:\y\_captures\%LATEST_FILE%"
"C:\y\_captures\%LATEST_FILE%"
REM pause
|
|
DarkLogixTexan and Proud Premium Member join:2008-10-23 Baytown, TX |
Well sometimes I've seen where just plain quoting it makes the variable looked at as a string instead of a variable. |
|
dave Premium Member join:2000-05-04 not in ohio |
dave
Premium Member
2015-Mar-5 4:17 pm
Maybe when there are multiple layers of parsers... batch files calling batch files, perhaps. |
|
DarkLogixTexan and Proud Premium Member join:2008-10-23 Baytown, TX |
DarkLogix
Premium Member
2015-Mar-11 12:00 pm
Guess its been more in VBS than in bat. |
|