

InterviewSolution
Saved Bookmarks
1. |
Solve : Setting a filename with spaces and extention into a variable (N00b)? |
Answer» <html><body><p>Hello, I am trying to <a href="https://interviewquestions.tuteehub.com/tag/move-548879" style="font-weight:bold;" target="_blank" title="Click to know more about MOVE">MOVE</a> files from one directoy to another, and I seem to be failing on filenames with spaces.<br/><br/>I tried the below, but I keep setting this <a href="https://interviewquestions.tuteehub.com/tag/variable-772077" style="font-weight:bold;" target="_blank" title="Click to know more about VARIABLE">VARIABLE</a> ( %1) to only the first part of a filename before the spaces:<br/><br/> <br/><br/><br/>EXAMPLE:<br/>dir "x:\move" /b >list.txt<br/>for /f %%a in (.\list.txt) do call :<a href="https://interviewquestions.tuteehub.com/tag/main-552371" style="font-weight:bold;" target="_blank" title="Click to know more about MAIN">MAIN</a> %%a<br/><br/>goto eol<br/><br/>:main<br/>pause<br/>set file=%1<br/><br/><br/>rename "x:\move\%file%" "s:\move\%file%"Try this:<br/><br/><strong>for /f "usebackq" %%a in (.\list.txt) do call :main %%a</strong><br/><br/>The <strong>usebackq</strong> option allows long file names, as long as quotes are used around the name.I tried :<br/><br/> Quote from: killerb255 on February 22, 2010, 12:53:01 PM</p><blockquote><strong>for /f "usebackq" %%a in (.\list.txt) do call :main %%a</strong><br/><br/></blockquote> <br/>but the %1 variable still has only the first 'word' (up to the filename's space)<br/><br/> <br/> Quote from: schreibman on February 22, 2010, 03:53:19 PM<blockquote>I tried :<br/><br/>but the %1 variable still has only the first 'word' (up to the filename's space)<br/><br/> <br/><br/></blockquote> That would be because %# is delimited by spaces. The file name needs to be wrapped in quotes. For example:<br/>C:\Documents and Settings\My Documents\<br/>%1=C:\Documents<br/>%2=and<br/>%3=Settings\My<br/>%4=Documents\To eliminate it from using a space as a delimiter (which is the default), try this:<br/><br/>for /f "usebackq tokens=1 delims={" %%a in (.\list.txt) do call :main %%a<br/><br/>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).<br/><br/>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?)<br/> Quote<blockquote>Hello, I am trying to move files from one directoy to another, and I seem to be failing on filenames with spaces.<br/></blockquote> <br/>Not sure why you have a <em>rename</em> in your <a href="https://interviewquestions.tuteehub.com/tag/code-25512" style="font-weight:bold;" target="_blank" title="Click to know more about CODE">CODE</a>, but you don't need it. This little snippet should take care of your space as delimiter and the quotes.<br/><br/> Code: <a>[Select]</a>echo off<br/>for /f "tokens=* delims=" %%a in ('dir /b x:\move') do (<br/> move "x:\move\%%~nxa" "s:\move\%%~nxa" <br/>)<br/><br/>Good luck. <br/> Quote from: killerb255 on February 22, 2010, 04:02:32 PM<blockquote>To eliminate it from using a space as a delimiter (which is the default), try this:<br/><br/>for /f "usebackq tokens=1 delims={" %%a in (.\list.txt) do call :main %%a<br/><br/>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).<br/><br/>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?)<br/><br/></blockquote> Quote from: schreibman on February 22, 2010, 03:53:19 PM<blockquote>I tried :<br/><br/>but the %1 variable still has only the first 'word' (up to the filename's space)<br/><br/> <br/><br/></blockquote> 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<blockquote>Not sure why you have a <em>rename</em> in your code,<br/></blockquote> <br/>Sidewinder, Cause I'm a <a href="https://interviewquestions.tuteehub.com/tag/n00b" style="font-weight:bold;" target="_blank" title="Click to know more about N00B">N00B</a>!<br/><br/>In anycase, you "suggestion" (basically your perfect re-write!) both did the trick perfectly and educated me!<br/><br/>Thanks so much for your help!</body></html> | |