1.

Solve : Setting a filename with spaces and extention into a variable (N00b)?

Answer»

Hello, I am trying to MOVE files from one directoy to another, and I seem to be failing on filenames with spaces.

I tried the below, but I keep setting this VARIABLE  (  %1) to only the first part of a filename before the spaces:

 


EXAMPLE:
dir "x:\move" /b >list.txt
for /f %%a in (.\list.txt) do call :MAIN %%a

goto eol

:main
pause
set file=%1


rename "x:\move\%file%"   "s:\move\%file%"Try this:

for /f "usebackq" %%a in (.\list.txt) do call :main %%a

The usebackq option allows long file names, as long as quotes are used around the name.I tried :

Quote from: killerb255 on February 22, 2010, 12:53:01 PM

for /f "usebackq" %%a in (.\list.txt) do call :main %%a


but the  %1 variable still has only the first 'word' (up to the filename's space)

 
Quote from: schreibman on February 22, 2010, 03:53:19 PM
I tried :

but the  %1 variable still has only the first 'word' (up to the filename's space)

 

That would be because %# is delimited by spaces. The file name needs to be wrapped in quotes. For example:
C:\Documents and Settings\My Documents\
%1=C:\Documents
%2=and
%3=Settings\My
%4=Documents\To eliminate it from using a space as a delimiter (which is the default), try this:

for /f "usebackq tokens=1 delims={" %%a in (.\list.txt) do call :main %%a

After the equal sign, you can use any character that's not being used in your file name list (so don't use a left brace ({) if your list has left braces).

The idea is to tell the batch NOT to use a space, as it'll use a space unless you specify otherwise (is there a way to specify a carriage-return as a delimiter?)
Quote
Hello, I am trying to move files from one directoy to another, and I seem to be failing on filenames with spaces.

Not sure why you have a rename in your CODE, but you don't need it. This little snippet should take care of your space as delimiter and the quotes.

Code: [Select]echo off
for /f "tokens=* delims=" %%a in ('dir /b x:\move') do (
  move "x:\move\%%~nxa" "s:\move\%%~nxa" 
)

Good luck. 
Quote from: killerb255 on February 22, 2010, 04:02:32 PM
To eliminate it from using a space as a delimiter (which is the default), try this:

for /f "usebackq tokens=1 delims={" %%a in (.\list.txt) do call :main %%a

After the equal sign, you can use any character that's not being used in your file name list (so don't use a left brace ({) if your list has left braces).

The idea is to tell the batch NOT to use a space, as it'll use a space unless you specify otherwise (is there a way to specify a carriage-return as a delimiter?)

Quote from: schreibman on February 22, 2010, 03:53:19 PM
I tried :

but the  %1 variable still has only the first 'word' (up to the filename's space)

 

Problem is highlighted. User input is delimited by spaces, %1-??? and I don't think that can be changed. Quote from: Sidewinder on February 22, 2010, 07:40:05 PM
Not sure why you have a rename in your code,

Sidewinder, Cause I'm a N00B!

In anycase, you "suggestion" (basically your perfect re-write!)  both did the trick perfectly and educated me!

Thanks so much for your help!


Discussion

No Comment Found