DOS Batch file - Copy file based on filename elements

Posted by user1848356 on Stack Overflow See other posts from Stack Overflow or by user1848356
Published on 2012-11-23T19:36:22Z Indexed on 2012/11/26 11:06 UTC
Read the original article Hit count: 175

Filed under:
|
|
|
|

I need to sort alot of files based on their filename. I would like to use a batch file to do it. I do know what I want but I am not sure of the correct syntax.

Example of filenames I work with: (They are all in the same directory originally)

2012_W34_Sales_Store001.pdf  
2012_W34_Sales_Store002.pdf  
2012_W34_Sales_Store003.pdf  

2012_November_Sales_Store001.pdf  
2012_November_Sales_Store002.pdf  
2012_November_Sales_Store003.pdf

I would like to extract the information that are located between the "_" signs and put them into a different variable each time. The lenght of the informations contained between the _ signs will be different everytime.

Example:
var1="2012"
var2="W34" (or November)
var3="Sales"
var4="001"

If I am able to do this, I could then copy the files to the appropriate directory using

move %var1%_%var2%_%var3%_%var4%.pdf z:\%var3%\%var4%\%var1%\%var2%

It would need to loop because I have Store001 to Store050. Also, there are not only Sales report, many others are available.

I hope I am clear.

Please help me realize this batchfile!

© Stack Overflow or respective owner

Related posts about variables

Related posts about script