?
Solved

batch script on windows

Posted on 2011-02-21
10
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 70

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 70

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 70

Accepted Solution

by:
Qlemo earned 2000 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

771 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