

InterviewSolution
Saved Bookmarks
1. |
Solve : DOS script to read a text file? |
Answer» <html><body><p>Hi<br/><br/>I need a batch file to read the 1st line in a text and save that as another text file. <br/><br/>For eg. I have the 1st line in the text as abcd :1234<br/><br/>I need to read from 7th character as 1234 and store it in a variable and use this as name for another text file 1234.txt<br/><br/>I am using for/f statement and i am not able to extract the 7th character. <br/><br/>Please help Try <a href="https://interviewquestions.tuteehub.com/tag/looking-1079184" style="font-weight:bold;" target="_blank" title="Click to know more about LOOKING">LOOKING</a> at <br/>set /?<br/>on the command prompt for more information on getting specific characters of variables.<br/><br/><br/><br/>This code would extract the 7th character of a string.<br/>set variable=%original:~6,1% Quote from: dos_help on March 02, 2010, 05:02:12 PM</p><blockquote>For eg. I have the 1st line in the text as abcd :1234<br/><br/>I need to read from 7th character as 1234 and store it in a variable <br/>I am using for /f statement and I am not able to extract from the 7th character. <br/></blockquote> <br/><br/><br/>C:\>type seven.bat<br/><br/> Code: <a>[Select]</a>echo off<br/><br/>setlocal enabledelayedexpansion<br/>for /f "delims=" %%i in (seven.txt) do (<br/>echo %%i<br/>set seven=%%i<br/>set seven=!seven:~6,4!<br/>echo seven = !seven!<br/>echo seven > !seven!.txt<br/>dir !seven!.txt<br/>)<br/><strong>Output:</strong><br/><br/>C:\> seven.bat<br/>abcd :1234<br/>seven = 1234<br/> Volume in drive C has no label.<br/> Volume Serial Number is F4A3-D6B3<br/><br/> Directory of C:\<br/><br/>03/02/2010 07:24 PM 8 1234.txt<br/> 1 File(s) 8 bytes<br/> 0 Dir(s) 299,228,622,848 bytes free<br/><br/>C:\><br/><br/><strong>Input:</strong><br/><br/>C:\>type seven.txt<br/>abcd :1234<br/><br/>C:\><br/><br/>reference:<br/><a href="http://www.dostips.com/DtTipsStringManipulation.php#Snippets.RightString">http://www.dostips.com/DtTipsStringManipulation.php#Snippets.RightString</a><br/><br/> Quote from: dos_help on March 02, 2010, 05:02:12 PM<blockquote>I am using for/f statement and i am not able to extract the 7th character. <br/><br/>Please help <br/></blockquote> use for loop with variable %%a, set delims=":" , tokens=1,2 , then get %%b inside the for loop Quote from: dos_help on March 02, 2010, 05:02:12 PM<blockquote>I need a batch file to read the 1st line in a text and save that as another text file. <br/></blockquote> <br/>C:\>type ghost32.bat<br/><br/> Code: <a>[Select]</a>echo off<br/><br/>setlocal enabledelayedexpansion<br/>for /f "tokens=1,2 delims=:" %%a in (seven.txt) do (<br/>echo %%a %%b<br/>set seven=%%b<br/>echo seven = !seven!<br/>echo seven > "!seven!.txt"<br/>dir "!seven!.txt"<br/>)<br/>rem use for loop with variable %%a, set delims=":" , tokens=1,2 ,<br/>rem then get %%b inside the for loop<br/><br/><strong>Output:</strong><br/><br/>C:\> ghost32.bat<br/>abcd 1234<br/>seven = 1234<br/> Volume in drive C has no label.<br/> Volume Serial Number is F4A3-D6B3<br/><br/> Directory of C:\<br/><br/>03/02/2010 08:35 PM 8 <strong>1234 .txt</strong><br/> 1 File(s) 8 bytes<br/> C:\> Quote from: dos_help on March 02, 2010, 05:02:12 PM<blockquote>I need a batch file to read the 1st line in a text and save that as another text file. <br/></blockquote> <br/><br/>C:\>type sed32.bat<br/><br/> Code: <a>[Select]</a>echo off<br/>sed s/"abcd :1234"/1234/g seven.txt 1> seven7.txt<br/><br/>set /p seven=<seven7.txt<br/>echo seven=%seven%<br/>type seven7.txt<br/><br/>echo Hello > %seven%.txt<br/><br/>dir "%seven%.txt"<br/><strong>Output:</strong><br/><br/><br/>C:\>sed32.bat<br/>seven=1234<br/>1234<br/> Volume in drive C has no label.<br/> Volume Serial Number is F4A3-D6B3<br/><br/> Directory of C:\<br/><br/>03/02/2010 08:35 PM 8 <strong>1234 .txt</strong><br/> 1 File(s) 8 bytes<br/> 0 Dir(s) 299,224,727,552 bytes free<br/><br/>C:\>this looks so familar, greg = bill ? Quote from: ghostdog74 on March 02, 2010, 09:07:46 PM<blockquote>this looks so familar, greg = bill ?<br/></blockquote> <br/>he goes by <a href="https://interviewquestions.tuteehub.com/tag/many-554478" style="font-weight:bold;" target="_blank" title="Click to know more about MANY">MANY</a> names, apparently. Quote from: BC_Programmer on March 02, 2010, 09:24:38 PM<blockquote>he goes by many names, apparently.<br/></blockquote> i wonder why he has so many names. did he get banned everytime? Quote from: dos_help on March 02, 2010, 05:02:12 PM<blockquote>I need a batch file to read the 1st line in a text and save that as another text file. <br/></blockquote> <br/>I'm sorry your thread has veered of topic.<br/><br/>I hope you have enough information to solve your problem.<br/><br/>Off topic posts should use <a href="https://interviewquestions.tuteehub.com/tag/private-11896" style="font-weight:bold;" target="_blank" title="Click to know more about PRIVATE">PRIVATE</a> mail.<br/><br/>Good luck Quote from: ghostdog74 on March 02, 2010, 09:31:19 PM<blockquote>i wonder why he has so many names. did he get banned everytime?<br/></blockquote> I think so. He's used so many accounts so far, there's no real difference here. Quote from: Helpmeh on March 03, 2010, 03:47:47 PM<blockquote> He's used so many accounts so far, there's no real difference here.<br/></blockquote> <br/>Off topic comments should be sent by private mail.<br/><br/>p.s.: BillRichardson's account is still <a href="https://interviewquestions.tuteehub.com/tag/active-367234" style="font-weight:bold;" target="_blank" title="Click to know more about ACTIVE">ACTIVE</a>.<br/><br/> Summary - BillRichardson Picture/Text <br/>Name: BillRichardson <br/>Posts: 194 (2.694 per day) <br/>Position: Intermediate <br/>Thanked: 15 <br/>Date Registered: December 21, 2009, 08:19:08 AM <br/><br/><br/>--------------------------------------------------------------------------------<br/> <br/>There <a href="https://interviewquestions.tuteehub.com/tag/appears-881937" style="font-weight:bold;" target="_blank" title="Click to know more about APPEARS">APPEARS</a> to be no reason for a ban.</body></html> | |