

InterviewSolution
Saved Bookmarks
1. |
Solve : Create line based on filename or text? |
Answer» <html><body><p>I have one .txt file and one .bat file:<br/><br/>VARIABLE.TXT<br/>with one <a href="https://interviewquestions.tuteehub.com/tag/line-239358" style="font-weight:bold;" target="_blank" title="Click to know more about LINE">LINE</a>:<br/> Code: <a>[Select]</a>variabletext<br/>CREATEFILE.BAT<br/><br/>I want CREATEFILE.BAT to create a file with a filename based on the content (variabletext) of VARIABLE.TXT<br/><br/>Does anyone know if this is possible and what command to set in CREATEFILE.BAT?<br/>Many <a href="https://interviewquestions.tuteehub.com/tag/thanks-665909" style="font-weight:bold;" target="_blank" title="Click to know more about THANKS">THANKS</a> in <a href="https://interviewquestions.tuteehub.com/tag/advance-850602" style="font-weight:bold;" target="_blank" title="Click to know more about ADVANCE">ADVANCE</a>!<br/><br/>David<br/>Look at<br/>set /?<br/>at the command prompt for more information.<br/><br/>Set /p contents=echo %contents%<br/><br/>NOTE: The above will only work with the FIRST line. If you want to work with all lines, that's a whole different story.<br/>C:\>type createfile.bat<br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/>for /f "delims=" %%i in (variable.txt) do (<br/>echo %%i<br/>set contents=%%i<br/>echo contents=!contents!<br/>echo newfile > !contents!.txt<br/>dir "!contents!.txt"<br/>)<br/><strong>Output:</strong><br/><br/>C:\>createfile.bat<br/>mynewfile<br/>contents=mynewfile<br/> Volume in drive C has no label.<br/> Volume Serial Number is F4A3-D6B3<br/><br/> <a href="https://interviewquestions.tuteehub.com/tag/directory-11615" style="font-weight:bold;" target="_blank" title="Click to know more about DIRECTORY">DIRECTORY</a> of C:\<br/><br/>03/03/2010 05:58 PM 10 mynewfile.txt<br/> 1 File(s) 10 bytes<br/> 0 Dir(s) 299,179,556,864 bytes free<br/>C:\>greg,<br/><br/>Your example worked like a charm. You guys really helped me out.<br/>I appreciate it very much!<br/><br/>David</p></body></html> | |