Solved

batch script on windows

Posted on 2011-02-21
10
343 Views
Last Modified: 2012-05-11
Hi,
I am trying to write a batch script that needs to work on windows server 2003.

I have xml order files which need moving to their appropriate folders.

The easy bit.
- move files to a temp directory

The not so easy bit.
Create a list of files
- for each file in the list: build a directory path from the file name.
e.g. if the file is 12345678_orders_test.xml, then the file would be moved to F:\12345678\orders

0
Comment
Question by:failed
  • 4
  • 3
  • 3
10 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 34944696
We need some more explanation here:
1. Do you create the file list manually?
2. The files in the list need to be moved to those directories derived from the file name?
3. Is there a variable amount of underscores, and each one is a directory name separator?

If all of that is true:
@echo off
setlocal EnableDelayedExpansion
pushd c:\SourceFolder
for /F "tokens=*" %%F in (filelist.txt) do (
   set newpath=%%~F
   set newpath=!newpath:_=\!
   xcopy "%%~F" "!newpath!"
)
popd

Open in new window

xcopy will create the folder tree, if necessary.
0
 
LVL 22

Expert Comment

by:Christopher McKay
ID: 34944721
Have yo considered trying this:


dir *.xml >> files.log
for /f "tokens=1,2* delims=_" %%i in (files.log) do (
mkdir f:\<path>\%%i\%%j
xcopy %%i_%%j_* f:\<path>\%%i\%%j\
)
del files.log

Open in new window

0
 
LVL 22

Expert Comment

by:Christopher McKay
ID: 34944730
I'm slow again.

Sorry Qlemo.

Time to go get more coffee.

:o)

Bartender_1
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:failed
ID: 34944746
1. I would like it to generate a list of files in the current directory and iterate through them
2. Each file wants to be moved to the directory path derived from its file name
3. the file names only have 2 underscores, characters 1-8 create the first directory and 10-15 create the subdirectory
Thanks
0
 
LVL 22

Expert Comment

by:Christopher McKay
ID: 34944780
and my batch file has an error in it again. Already. I really should learn to proof read a few times instead of just once to catch these errors.


dir /b *.xml >> files.log
for /f "tokens=1,2* delims=_" %%i in (files.log) do (
mkdir f:\<path>\%%i\%%j
xcopy %%i_%%j_* f:\<path>\%%i\%%j\
)
del files.log

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34944816
That would do it.

If you want to follow my example code, you just need to insert this line
> filelist.txt dir /b *.xml
between line 3 and 4.
My script is more flexible, Bartender's script works if there are more than two underscores, but they should NOT be used to create more folders.
0
 
LVL 22

Expert Comment

by:Christopher McKay
ID: 34944928
It should be noted, Qlemo is more proficient with batch scripting.
My batch file should work, however Qlemo's approach is more flexible as they have stated.

:o)

Bartender_1
0
 

Author Comment

by:failed
ID: 34950019
Qlemo, I have tried your solution.
It creates a list of files and creates a directory for each file.
For a file "12345678_ordrsp_test.xml" it creates a directory \12345678\ordrsp\test\
I only wanted the first two parts \12345678\ordrsp\
Also I would like it to be copied to two locations.
And then deleted from original location.
The files are generated into C:\edi_out and not copying to C:\ftp and C:\edi_backup

So for the filename above a copy will be sent to C:\ftp\12345678\ordrsp\ and C:\edi_backup\12345678\ordrsp\
0
 

Author Comment

by:failed
ID: 34950154
There would need to be a first step where the files have .xml appended to the name
Also the filenames would be like "12345678_ordrsp.021546.xml"
Thanks
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 35048316
I've tested my script (again), and I cannot agree. "12345678_orders_test.xml" is trying to be copied into "12345678\orders\test.xml" - no additional "test" folder created here.

Please stop changing your requirements each time you post. Your last one renders Bartender's answer useless. And it is not fitting into the orignal requirement in any way anymore. We can fix the "move" part we both seem to have missed, but we are more confused now than before.

Your original files are
   12345678_ordrsp.021546
   87654321_ordrsp.471147
You want them to have a .xml appended, then copied/moved to two different locations.
The target folders in both locations should be generated by the part up to the first dot, and an underscore represents a folder structure.

Assuming all that is true, and you do not need the filelist.txt, the batch file might look like this:
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=*" %%F in ('dir C:\edi_out\*_*.* /b /a:-d') do (
   set newpath=%%~nF
   set newpath=!newpath:_=\!
   set newname=%%~xF
   set newname=!newname:~1!.xml
   for %%D in (C:\edi_backup c:\ftp) do xcopy "%%~fF" "%%D\!newpath!\!newname!"
   del "%%~fF"
)

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Numerous times I have been asked this questions that what is it that makes my machine log on so slow, there have been cases where computers took 23 minute exactly after taking password and getting to the desktop. Interesting thing was the fact th…
Learn about cloud computing and its benefits for small business owners.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question