|
Answer» Dear all,
I require some urgent help on the BATCH file functionality.
I need to have a batch file that automatically creates a folder based on the value of a filename and copy the file into the folder. The xxxx\yyyy have many values
Example:
Filename = “Test - xxxx_yyyy.pdf”
Folder = MIS\xxxx\yyyy\2006\
I do not know how to write the command, really appericate if anyone can help me on this. Thanks very much. You might have left out where this file name comes from, so I took it upon myself to source the files from a directory listing. In any case this is just a matter of playing around with the tokens generated by a FOR statement.
Code: [Select]@echo off for /f "tokens=1-4 delims=-_. " %%i in ('dir /b *.pdf') do ( md "MIS\%%j\%%k\2006" copy "%%i - %%j_%%k.%%L" "MIS\%%j\%%k\2006" ) Hi
Thanks for your help sidewinder, the above commands really works for fixed test, xxxx and yyyy alpha, but what happen if I have many many different files name format that exists in the same root directory? The distinct sepration between those x, y, and Test is the - and _.
Example: xxxx could be xx xxx xxx or xxx xx xxxx or xxxxxxxx yyyy could be yyy yy yyyy or yyyy yyy yy or yyyyyyy y Test could be Test Test Test
Anyone can help please. Thanks very much.You could try removing the space character from the delimiter list and adding a path to point to the right directory.
for /f "tokens=1-4 delims=-_." %%i in ('dir /b \directoryname\*.pdf') do (
At this point %%i should equal all the Test iterations; %%j should equal all the XXXX iterations; %%k should equal all the YYYY iterations and %%k should equal pdf.
You should be able to construct the make directory and copy statements from these tokens.
Good luck. 8-)
PS. One of the problems with batch code is the data items have no NAMES, making readability difficult. When batch solutions become complex, a better way might be using a script language. VBScript and JScript are ALREADY installed on Windows machines free of charge. There are many other script languages (Perl, Python, Rexx, Kix, Ruby, etc) that are free to DOWNLOAD and use. Note that each language has it's strengths and weaknesses.
|