

InterviewSolution
Saved Bookmarks
1. |
Solve : simple batch file needs little modding? |
Answer» <html><body><p>Hi there I need to create a batch file that will read a text document and create a file for each line of text I have one that almost works but the trouble is the lines of texts have spaces in them and it is creating a file for each section of text so if i have a line of text that reads 'I want only one file' it will create files called I, want, only, one and file the script I am using is<br/><br/>for /f %%i in (file.txt) do echo off> %%i <br/><br/>have tried <br/><br/>for /f %%i in (file.txt) do echo off> "%%i" <br/><br/>and <br/><br/>for /f "%%i" in (file.txt) do echo off> "%%i" <br/><br/>with no luck any help much apreciated.<br/>thanks, James.Try:<br/><br/>echo off<br/>for /f "delims=" %%i in (file.txt) do echo. > %%i<br/><br/>or if you're running it from the command prompt,<br/><br/>for /f "delims=" %i in (file.txt) do echo. > %i<br/><br/> Code: <a>[Select]</a>for /f "tokens=*" %%i in (file.txt) do echo %%i<br/><br/><br/>echoed the list of filenames I had in the file (including spaces).<br/><br/>I'm pretty <a href="https://interviewquestions.tuteehub.com/tag/sure-656539" style="font-weight:bold;" target="_blank" title="Click to know more about SURE">SURE</a> echo off will echo nothing to the file though. Quote from: Helpmeh on March 10, 2010, 04:14:15 PM</p><blockquote>Try:<br/><br/>echo off<br/>for /f "delims=" %%i in (file.txt) do echo. > %%i<br/><br/>or if you're running it from the command prompt,<br/><br/>for /f "delims=" %i in (file.txt) do echo. > %i<br/><br/><br/></blockquote> HEY! That was my 3000th post!!!<br/><br/>Anyway, BC, wouldn't "tokens=*" and "delims=" pretty much do the same thing?C:\batch><a href="https://interviewquestions.tuteehub.com/tag/type-238192" style="font-weight:bold;" target="_blank" title="Click to know more about TYPE">TYPE</a> wonker.bat<br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/>for /f "delims=" %%i in (file.txt) do (<br/>echo %%i<br/>set newf=%%i<br/>echo Turn off > "!newf!.txt"<br/>dir "!newf!.txt"<br/>)<br/><strong>Output:</strong><br/><br/>C:\batch> wonker.bat<br/>wonker<br/> Volume in drive C has no label.<br/> Volume Serial Number is F4A3-D6B3<br/><br/> Directory of C:\batch<br/><br/>03/10/2010 05:28 PM 11 wonker.txt<br/> 1 File(s) 11 bytes<br/> 0 Dir(s) 298,567,569,408 bytes free<br/>hope<br/> Volume in drive C has no label.<br/> Volume Serial Number is F4A3-D6B3<br/><br/> Directory of C:\batch<br/><br/>03/10/2010 05:28 PM 11 hope.txt<br/> 1 File(s) 11 bytes<br/> 0 Dir(s) 298,567,569,408 bytes free<br/>works<br/> Volume in drive C has no label.<br/> Volume Serial Number is F4A3-D6B3<br/><br/> Directory of C:\batch<br/><br/>03/10/2010 05:28 PM 11 works.txt<br/> 1 File(s) 11 bytes<br/> 0 Dir(s) 298,567,569,408 bytes free<br/><br/>C:\batch><br/><br/><strong>Input:</strong><br/><br/><br/>C:\batch>type file.txt<br/>wonker<br/>hope<br/>works<br/>C:\batch>thanks to all who replied from all your suggestions I tried the easiest looking one first and with a little modding got it working perfect I ended up using <br/><br/>for /f "tokens=*" %%i in (file.txt) do echo off> %%i<br/><br/>not that its that important for me at the moment but I have in past wanted to do same thing but to <a href="https://interviewquestions.tuteehub.com/tag/make-249948" style="font-weight:bold;" target="_blank" title="Click to know more about MAKE">MAKE</a> folders instead of files but used to have same problem with it creating new folder for each word so thought Id <a href="https://interviewquestions.tuteehub.com/tag/see-630247" style="font-weight:bold;" target="_blank" title="Click to know more about SEE">SEE</a> if this would now work same for folders so I tried <br/><br/>for /f "tokens=*" %%i in (file.txt) do mkdir %%i<br/><br/>but this still made the folders for each word any ideas why that is?<br/><br/>Wrap it in quotes. Like this:<br/><br/>"%%i"brilliant had tried similar when trying to mod my original script for making files which <a href="https://interviewquestions.tuteehub.com/tag/didnt-2044153" style="font-weight:bold;" target="_blank" title="Click to know more about DIDNT">DIDNT</a> work so didnt try doing same again, but it worked on that one thanks so much for all your help this forum is great, get plenty of quick succesful responses. I'm just beginning with trying to learn some basics with making scripts and have got a few ideas of scripts I would like to create so no doubt you will here from me again within the next few days.<br/>thanks again, James.</body></html> | |