Solved

batch script on windows

Posted on 2011-02-21
10
329 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 68

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:Bartender_1
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:Bartender_1
ID: 34944730
I'm slow again.

Sorry Qlemo.

Time to go get more coffee.

:o)

Bartender_1
0
 

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:Bartender_1
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 68

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:Bartender_1
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 68

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Learn about cloud computing and its benefits for small business owners.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now