|
|
Question : Renaming Multiple files through the use of a batch file
|
|
We have to change the way we receive edi messages from our trading partners, and we have successfully managed to receive the messages.
now the problem is, they are in a format of :- samptest.txt.002 samptest.txt.003 etc
we require these files to be in a format such as samptest001.edi samptest002.edi etc
for an additional process to understand and convert these messages to put into an oracle database.
as we can receive anything between 30-50 messages a day, manually renaming the files is an inefficient use of resources.
I think what we need is a batch file that will rename the original files and with the numbers at the end to differentiate between filenames, with the .edi extension.
anyone any ideas?
|
Answer : Renaming Multiple files through the use of a batch file
|
|
Use a for command at the command prompt as follows:
C:\>for /f "tokens=3 delims=." %a in ('dir /B samptest.txt.*') do (rename samptest.txt.%a samptest%a.edi)
What this does is it executes the 'dir /B samptest.txt.*' command which outputs the following:
C:\>dir /B samptest.txt.* samptest.txt.002 samptest.txt.003
Instead of displaying the output to the command window it parses each line looking for the third token, since I've set the delimiter to '.' the third token will be the second file extension (example: 002 or 003). Once the third token is found for that line it stores that value in the variable %a and then processes the do command. The do command will be processed once for each line the 'dir /B' command returns and is able to resolve a third token.
So the result is that the for statement renames all the files to the format you want.
NOTE: to use the "for" command in a batch file it will be necessary to double up on the % sign in front of the variable name (since batch files also allow variables... a little confusing but it works). The above format will work well for testing at the command prompt, use the following in a batch file if you that is how you plan on setting upt he task: for /f "tokens=3 delims=." %%a in ('dir /B samptest.txt.*') do (rename samptest.txt.%%a samptest%%a.edi)
Let me know if you like it.
Enjoy, Aaron.
|
|
|
|
|