dslreports logo
 
    All Forums Hot Topics Gallery
spc
Search similar:


uniqs
545

inGearX
3.1415 9265
join:2000-06-11
New York

1 edit

inGearX

Member

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

inGearX

Member

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
 

aurgathor
join:2002-12-01
Lynnwood, WA

aurgathor to inGearX

Member

to inGearX
Use "around file or path . names"

mmainprize
join:2001-12-06
Houghton Lake, MI

mmainprize to inGearX

Member

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

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.

mmainprize
join:2001-12-06
Houghton Lake, MI

mmainprize to inGearX

Member

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

19579823 (banned) to inGearX

Member

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

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 See Profile.

DarkLogix
Texan and Proud
Premium Member
join:2008-10-23
Baytown, TX

DarkLogix to mmainprize

Premium Member

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

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
 

DarkLogix
Texan and Proud
Premium Member
join:2008-10-23
Baytown, TX

DarkLogix

Premium Member

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

Maybe when there are multiple layers of parsers... batch files calling batch files, perhaps.

DarkLogix
Texan and Proud
Premium Member
join:2008-10-23
Baytown, TX

DarkLogix

Premium Member

Guess its been more in VBS than in bat.