

InterviewSolution
Saved Bookmarks
1. |
Solve : .bat or .vbs Find within text and Rename File? |
Answer» <html><body><p>I have outputted some text files all in the same directory. Each .txt <a href="https://interviewquestions.tuteehub.com/tag/file-11330" style="font-weight:bold;" target="_blank" title="Click to know more about FILE">FILE</a> has within a group number, this number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXXXX i need the script to find this RXC number and rename the file to its corresponding group number, then do the same for all files in the same directory.<br/><br/>would really like this to be a .bat but a .vbs will also work.<br/><br/>Thanks in advance,<br/>Joe Quote from: jmituzas on August 31, 2010, 12:56:20 PM</p><blockquote>number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXXXXi </blockquote> <br/>C:test>type rxc.bat<br/>echo off<br/><br/><a href="https://interviewquestions.tuteehub.com/tag/rem-613824" style="font-weight:bold;" target="_blank" title="Click to know more about REM">REM</a> dir /b rxc* >> rxc.txt<br/>echo. >> rxc.txt<br/>type rxc.txt<br/><br/>setlocal enabledelayedexpansion<br/>for /f delims= %%i in (rxc.txt) do (<br/>set x=%%i<br/>set x=!x:~3!<br/>echo !x!<br/>rem copy %%i !x!<br/>rem ren %%i !x!<br/>)<br/>Output:<br/><br/>C:test>rxc.bat<br/>rcx12345<br/>rcx23456<br/>rcx34567<br/><br/>12345<br/>23456<br/>34567<br/><br/><br/>C:test>Hmmmm!<br/><br/> Quote from: OP<blockquote>I have outputted some text files all in the same directory. Each .txt file has <strong><strong>within</strong></strong> a group number</blockquote> <br/>I think this means that the group number (RCXnnnnn) is part of the contents of the text file, in other words is part of the text not of the filename which could be anything.. Code: <a>[Select]</a>Set objFS = CreateObject("Scripting.FileSystemObject")<br/>strFolder="c:\test"<br/>Set objFolder = objFS.GetFolder(strFolder)<br/>For Each strFile In objFolder.Files<br/> If objFS.GetExtensionName(strFile) = "txt" Then <br/> strFileName = strFile.Name<br/> Set objFile = objFS.OpenTextFile(strFileName)<br/> Do Until objFile.AtEndOfStream <br/> strLine=objFile.ReadLine<br/> If InStr(strLine,"RXC" ) > 0 Then<br/> number=Mid(strLine,4,5) 'Get 5 characters afterwards<br/> objFile.Close<br/> strFile.Name = Trim(number)&".txt" <br/> Exit Do <br/> End If <br/> Loop <br/> End If <br/>Next <br/> Quote from: T.C. on August 31, 2010, 04:10:48 PM<blockquote><br/>I think this means that the group number (RCXnnnnn) is part of the contents of the text file, in other words is part of the text not of the filename which could be anything.<br/></blockquote> <br/><br/>C:test>Display rxc2.bat<br/><br/>echo off<br/>echo rxc12345 > rxc1.txt<br/>echo rxc23456 > rxc2.txt<br/>echo rxc34567 > rxc3.txt<br/><br/>echo. > run.txt<br/><br/>findstr rxc *.txt >> run.txt<br/><br/>echo Display run.txt<br/>type run.txt<br/>echo.<br/><br/>setlocal enabledelayedexpansion<br/>rem Will need double quote before skip and after delims=:<br/><br/>for /f skip=1 tokens=1,2 delims=: %%i in (run.txt) do (<br/>set filename=%%i<br/>set newname=%%j<br/>echo filename=!filename!<br/>echo newname=!newname!.txt<br/>rem copy !filename! !newname!<br/>rem ren !filename! !newname!<br/>)<br/>rem will need double quote around filename and newname for copy or ren<br/><br/>Output:<br/><br/>C:test>rxc2.bat<br/>Display run.txt<br/><br/>rxc1.txt:rxc12345<br/>rxc2.txt:rxc23456<br/>rxc3.txt:rxc34567<br/><br/>filename=rxc1.txt<br/>newname=rxc12345.txt<br/>filename=rxc2.txt<br/>newname=rxc23456.txt<br/>filename=rxc3.txt<br/>newname=rxc34567.txt<br/>C:test> Quote from: Fields on August 31, 2010, 06:13:31 PM<blockquote> Code: <a>[Select]</a>C:test>Display rxc2.bat<br/><br/>echo off<br/>echo rxc12345 > rxc1.txt<br/>echo rxc23456 > rxc2.txt<br/>echo rxc34567 > rxc3.txt<br/>echo. > run.txt<br/>findstr rxc *.txt >> run.txt<br/></blockquote> <br/><br/>how do you know OP has only RXCXXXXX on a line by itself? He <a href="https://interviewquestions.tuteehub.com/tag/may-557248" style="font-weight:bold;" target="_blank" title="Click to know more about MAY">MAY</a> have a line like this "troll RXC12345 is here".<br/>and then your script breaks.<br/> Quote from: ghostdog74 on August 31, 2010, 06:38:29 PM<blockquote><br/>How do you know OP has only RXCXXXXX on a line by itself? He may have a line like this nooutput RXC12345 is here.<br/>and then your script breaks.<br/><br/></blockquote> <br/>The tokens and delims can be adjusted to account for as many tokens as needed,<br/>We can also use more than one delims.<br/><br/>for /f skip=1 tokens=1,2 delims=: %%i in (run.txt) do (<br/><br/><br/><br/> Quote from: Fields on August 31, 2010, 06:52:19 PM<blockquote>The tokens and delims can be adjusted to account for as many tokens as needed,<br/>We can also use more than one delims.<br/><br/>for /f skip=1 tokens=1,2 delims=: %%i in (run.txt) do (<br/><br/><br/><br/><br/></blockquote> <br/>but do you know where RXC12345 will occur in the text? beginning of line? end of line? If it can occur anywhere in the text, then how do you go about setting your delims and tokens? Quote from: ghostdog74 on August 31, 2010, 06:58:32 PM<blockquote>But do you know where RXC12345 will occur in the text? beginning of line? end of line? If it can occur anywhere in the text, then how do you go about setting your delims and tokens?<br/></blockquote> <br/>The original code did not fail. There is a longer name for the new file. The adjustments I <a href="https://interviewquestions.tuteehub.com/tag/mentioned-1093838" style="font-weight:bold;" target="_blank" title="Click to know more about MENTIONED">MENTIONED</a> will <br/>work but not needed now. If OP request the adjustments we will do it.<br/><br/><br/><br/><br/> Quote from: Fields on August 31, 2010, 07:39:01 PM<blockquote>The original code did not fail. There is a longer name for the new file. <br/></blockquote> yes it does. Like your example rxc4.txt, you now have "nooutput rxc45678 is here" as the new file name. It should be "45678.txt" as the new file name. Here's a suggestion :<br/><br/> Code: <a>[Select]</a>for .... ( txtfile ) do (<br/> rem set delim to whitspaces.<br/> rem go through all the tokens, look for "RXC", <br/> rem once found, get the substring from 4th character onwards. <br/> rem create the new file.<br/>)<br/> Quote from: ghostdog74 on August 31, 2010, 08:03:00 PM<blockquote>yes it does. Like your example rxc4.txt, you now have \"nooutput rxc45678 is here\" as the new file name. It should be \"45678.txt\" as the new file name. Here\'s a suggestion :<br/><br/> Code: <a>[Select]</a>for .... ( txtfile ) do (<br/> rem set delim to whitspaces.<br/> rem go through all the tokens, look for \"RXC\", <br/> rem once found, get the substring from 4th character onwards. <br/> rem create the new file.<br/>)<br/></blockquote> <br/>Yes that will work but I will not look at all the tokens until the OP asks for it.<br/><br/>The long file name is ok with me.<br/> <br/>Many <a href="https://interviewquestions.tuteehub.com/tag/ops-1137414" style="font-weight:bold;" target="_blank" title="Click to know more about OPS">OPS</a> ask a question and never return.<br/><br/>By the way, I dont know enough VBS to get your VBS code to run. I have not spent much time with your VBS. Wll try later. Quote from: Fields on August 31, 2010, 08:52:57 PM<blockquote>The long file name is ok with me.<br/></blockquote> its not ok as OP's required specs. He wants to find the RXC number following RXC. (without other alphabets/words)<br/> <br/> Quote<blockquote>By the way, I dont know enough VBS to get your VBS code to run. I have not spent much time with your VBS. Wll try later.<br/></blockquote> you don't have to know much to learn how to run a vbscript. <br/> Code: <a>[Select]</a>cscript //nologo myscript.vbs<br/> Quote from: ghostdog74 on August 31, 2010, 05:57:20 PM<blockquote> Code: <a>[Select]</a>Set objFS = CreateObject(\"Scripting.FileSystemObject\")<br/> <br/> </blockquote> <br/>I was able to run your vbs file. The output did not come to the screen but the vbs did change the names of the original file names. <br/>The original file rxc4.txt with text [ nooutput rxc45678 is here]<br/>was changed to utput.txt and not to 45678.txt. This is clearly a flaw with the vbs solution<br/>The ops specs were not followed. Please correct<br/><br/><br/>C:\\test>type ghost831.vbs<br/>Set objFS = CreateObject(\"Scripting.FileSystemObject\")<br/>strFolder=\"c:\\test\"<br/>Set objFolder = objFS.GetFolder(strFolder)<br/>For Each strFile In objFolder.Files<br/> If objFS.GetExtensionName(strFile) = \"txt\" Then<br/> strFileName = strFile.Name<br/> Set objFile = objFS.OpenTextFile(strFileName)<br/> Do Until objFile.AtEndOfStream<br/> strLine=objFile.ReadLine<br/> If InStr(strLine,\"rxc\" ) > 0 Then<br/> number=Mid(strLine,4,5) \'Get 5 characters afterwards<br/> objFile.Close<br/> strFile.Name = Trim(number)&\".txt\"<br/> Exit Do<br/> End If<br/> Loop<br/> End If<br/>Next<br/><br/>Input:<br/><br/>C:test>findstr rxc *.txt<br/><br/>rxc1.txt:rxc12345<br/>rxc2.txt:rxc23456<br/>rxc3.txt:rxc34567<br/>rxc4.txt:nooutput rxc45678 is here<br/><br/>Output:<br/><br/>C:\\test>dir * | sort<br/><br/>09/01/2010 04:55 AM 11 12345.txt<br/>09/01/2010 04:55 AM 11 23456.txt<br/>09/01/2010 04:55 AM 11 34567.txt<br/>09/01/2010 04:55 AM 29 utput.txt<br/>09/01/2010 04:55 AM 101 1.txt.txt<br/><br/>C:test>type 12345.txt<br/>rxc12345<br/><br/>C:test>type utput.txt<br/>nooutput rxc45678 is here<br/><br/>C:test>type 1.txt.txt<br/><br/>rxc1.txt:rxc12345<br/>rxc2.txt:rxc23456<br/>rxc3.txt:rxc34567<br/>rxc4.txt:nooutput rxc45678 is here<br/>that's easy to change. So now, are you going to change your batch script to do the same?<br/> Code: <a>[Select]</a>Set objFS = CreateObject("Scripting.FileSystemObject")<br/>strFolder="c:\test"<br/>Set objFolder = objFS.GetFolder(strFolder)<br/>For Each strFile In objFolder.Files<br/> If objFS.GetExtensionName(strFile) = "txt" Then <br/> strFileName = strFile.Name<br/> Set objFile = objFS.OpenTextFile(strFileName)<br/> Do Until objFile.AtEndOfStream <br/> strLine=objFile.ReadLine<br/> m=InStr(strLine,"RXC" ) <br/> If m > 0 Then<br/> number=Mid(strLine,m+3,5) <br/> WScript.Echo number<br/> objFile.Close<br/> strFile.Name = Trim(number)&".txt" <br/> Exit Do <br/> End If <br/> Loop <br/> End If <br/>Next <br/> Quote from: ghostdog74 on September 01, 2010, 04:59:10 AM<blockquote>Thats easy to change. <br/></blockquote> <br/>Same error as before: 45678.txt is not created. utput.txt is created from the input file rxc4.txt with the<br/>line [ nooutput rxc45678 is here ] inside of the rxc4.txt file.<br/><br/>The fix for the ghost.vbs did not work.<br/><br/>C:test>Display ghost901.vbs<br/>Set objFS = CreateObject(Scripting.FileSystemObject)<br/>strFolder=c:test<br/>Set objFolder = objFS.GetFolder(strFolder)<br/>For Each strFile In objFolder.Files<br/> If objFS.GetExtensionName(strFile) = txt Then<br/> strFileName = strFile.Name<br/> Set objFile = objFS.OpenTextFile(strFileName)<br/> Do Until objFile.AtEndOfStream<br/> strLine=objFile.ReadLine<br/> If InStr(strLine,rxc ) > 0 Then<br/> number=Mid(strLine,4,5) Get 5 characters afterwards<br/> objFile.Close<br/> strFile.Name = Trim(number)&.txt<br/> Exit Do<br/> End If<br/> Loop<br/> End If<br/>Next<br/><br/><br/>C:test>cscript //nologo ghost901.vbs<br/><br/>Output:<br/><br/>09/01/2010 08:15 AM 11 12345.txt<br/>09/01/2010 08:15 AM 11 23456.txt<br/>09/01/2010 08:15 AM 11 34567.txt<br/>09/01/2010 08:15 AM 29 utput.txt<br/>09/01/2010 08:15 AM 101 1.txt.txt<br/><br/>C:test>type 12345.txt<br/>rxc12345<br/><br/>C:test>type utput.txt<br/>nooutput rxc45678 is here<br/><br/><br/>C:test>type 1.txt.txt<br/><br/>rxc1.txt:rxc12345<br/>rxc2.txt:rxc23456<br/>rxc3.txt:rxc34567<br/>rxc4.txt:nooutput rxc45678 is here</body></html> | |