Saved Bookmarks
| 1. |
Solve : .bat or .vbs Find within text and Rename File? |
|
Answer» I have outputted some text files all in the same directory. Each .txt FILE 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. number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXXXXi C:test>type rxc.bat echo off REM dir /b rxc* >> rxc.txt echo. >> rxc.txt type rxc.txt setlocal enabledelayedexpansion for /f delims= %%i in (rxc.txt) do ( set x=%%i set x=!x:~3! echo !x! rem copy %%i !x! rem ren %%i !x! ) Output: C:test>rxc.bat rcx12345 rcx23456 rcx34567 12345 23456 34567 C:test>Hmmmm! Quote from: OP I have outputted some text files all in the same directory. Each .txt file has within a group number 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: [Select]Set objFS = CreateObject("Scripting.FileSystemObject") strFolder="c:\test" Set objFolder = objFS.GetFolder(strFolder) For Each strFile In objFolder.Files If objFS.GetExtensionName(strFile) = "txt" Then strFileName = strFile.Name Set objFile = objFS.OpenTextFile(strFileName) Do Until objFile.AtEndOfStream strLine=objFile.ReadLine If InStr(strLine,"RXC" ) > 0 Then number=Mid(strLine,4,5) 'Get 5 characters afterwards objFile.Close strFile.Name = Trim(number)&".txt" Exit Do End If Loop End If Next Quote from: T.C. on August 31, 2010, 04:10:48 PM
C:test>Display rxc2.bat echo off echo rxc12345 > rxc1.txt echo rxc23456 > rxc2.txt echo rxc34567 > rxc3.txt echo. > run.txt findstr rxc *.txt >> run.txt echo Display run.txt type run.txt echo. setlocal enabledelayedexpansion rem Will need double quote before skip and after delims=: for /f skip=1 tokens=1,2 delims=: %%i in (run.txt) do ( set filename=%%i set newname=%%j echo filename=!filename! echo newname=!newname!.txt rem copy !filename! !newname! rem ren !filename! !newname! ) rem will need double quote around filename and newname for copy or ren Output: C:test>rxc2.bat Display run.txt rxc1.txt:rxc12345 rxc2.txt:rxc23456 rxc3.txt:rxc34567 filename=rxc1.txt newname=rxc12345.txt filename=rxc2.txt newname=rxc23456.txt filename=rxc3.txt newname=rxc34567.txt C:test> Quote from: Fields on August 31, 2010, 06:13:31 PM Code: [Select]C:test>Display rxc2.bat how do you know OP has only RXCXXXXX on a line by itself? He MAY have a line like this "troll RXC12345 is here". and then your script breaks. Quote from: ghostdog74 on August 31, 2010, 06:38:29 PM
The tokens and delims can be adjusted to account for as many tokens as needed, We can also use more than one delims. for /f skip=1 tokens=1,2 delims=: %%i in (run.txt) do ( Quote from: Fields on August 31, 2010, 06:52:19 PM The tokens and delims can be adjusted to account for as many tokens as needed, 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 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? The original code did not fail. There is a longer name for the new file. The adjustments I MENTIONED will work but not needed now. If OP request the adjustments we will do it. Quote from: Fields on August 31, 2010, 07:39:01 PM The original code did not fail. There is a longer name for the new file.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 : Code: [Select]for .... ( txtfile ) do ( rem set delim to whitspaces. rem go through all the tokens, look for "RXC", rem once found, get the substring from 4th character onwards. rem create the new file. ) Quote from: ghostdog74 on August 31, 2010, 08:03:00 PM 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 : Yes that will work but I will not look at all the tokens until the OP asks for it. The long file name is ok with me. Many OPS ask a question and never return. 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 The long file name is ok with me.its not ok as OP's required specs. He wants to find the RXC number following RXC. (without other alphabets/words) Quote 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.you don't have to know much to learn how to run a vbscript. Code: [Select]cscript //nologo myscript.vbs Quote from: ghostdog74 on August 31, 2010, 05:57:20 PM Code: [Select]Set objFS = CreateObject(\"Scripting.FileSystemObject\") 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. The original file rxc4.txt with text [ nooutput rxc45678 is here] was changed to utput.txt and not to 45678.txt. This is clearly a flaw with the vbs solution The ops specs were not followed. Please correct C:\\test>type ghost831.vbs Set objFS = CreateObject(\"Scripting.FileSystemObject\") strFolder=\"c:\\test\" Set objFolder = objFS.GetFolder(strFolder) For Each strFile In objFolder.Files If objFS.GetExtensionName(strFile) = \"txt\" Then strFileName = strFile.Name Set objFile = objFS.OpenTextFile(strFileName) Do Until objFile.AtEndOfStream strLine=objFile.ReadLine If InStr(strLine,\"rxc\" ) > 0 Then number=Mid(strLine,4,5) \'Get 5 characters afterwards objFile.Close strFile.Name = Trim(number)&\".txt\" Exit Do End If Loop End If Next Input: C:test>findstr rxc *.txt rxc1.txt:rxc12345 rxc2.txt:rxc23456 rxc3.txt:rxc34567 rxc4.txt:nooutput rxc45678 is here Output: C:\\test>dir * | sort 09/01/2010 04:55 AM 11 12345.txt 09/01/2010 04:55 AM 11 23456.txt 09/01/2010 04:55 AM 11 34567.txt 09/01/2010 04:55 AM 29 utput.txt 09/01/2010 04:55 AM 101 1.txt.txt C:test>type 12345.txt rxc12345 C:test>type utput.txt nooutput rxc45678 is here C:test>type 1.txt.txt rxc1.txt:rxc12345 rxc2.txt:rxc23456 rxc3.txt:rxc34567 rxc4.txt:nooutput rxc45678 is here that's easy to change. So now, are you going to change your batch script to do the same? Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject") strFolder="c:\test" Set objFolder = objFS.GetFolder(strFolder) For Each strFile In objFolder.Files If objFS.GetExtensionName(strFile) = "txt" Then strFileName = strFile.Name Set objFile = objFS.OpenTextFile(strFileName) Do Until objFile.AtEndOfStream strLine=objFile.ReadLine m=InStr(strLine,"RXC" ) If m > 0 Then number=Mid(strLine,m+3,5) WScript.Echo number objFile.Close strFile.Name = Trim(number)&".txt" Exit Do End If Loop End If Next Quote from: ghostdog74 on September 01, 2010, 04:59:10 AM Thats easy to change. Same error as before: 45678.txt is not created. utput.txt is created from the input file rxc4.txt with the line [ nooutput rxc45678 is here ] inside of the rxc4.txt file. The fix for the ghost.vbs did not work. C:test>Display ghost901.vbs Set objFS = CreateObject(Scripting.FileSystemObject) strFolder=c:test Set objFolder = objFS.GetFolder(strFolder) For Each strFile In objFolder.Files If objFS.GetExtensionName(strFile) = txt Then strFileName = strFile.Name Set objFile = objFS.OpenTextFile(strFileName) Do Until objFile.AtEndOfStream strLine=objFile.ReadLine If InStr(strLine,rxc ) > 0 Then number=Mid(strLine,4,5) Get 5 characters afterwards objFile.Close strFile.Name = Trim(number)&.txt Exit Do End If Loop End If Next C:test>cscript //nologo ghost901.vbs Output: 09/01/2010 08:15 AM 11 12345.txt 09/01/2010 08:15 AM 11 23456.txt 09/01/2010 08:15 AM 11 34567.txt 09/01/2010 08:15 AM 29 utput.txt 09/01/2010 08:15 AM 101 1.txt.txt C:test>type 12345.txt rxc12345 C:test>type utput.txt nooutput rxc45678 is here C:test>type 1.txt.txt rxc1.txt:rxc12345 rxc2.txt:rxc23456 rxc3.txt:rxc34567 rxc4.txt:nooutput rxc45678 is here |
|