Solved

batch script on windows

Posted on 2011-02-21
10
353 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Questions about DHCP migration 5 118
AD account Auto logoff 1 57
windows Server 2003 in 2017 10 109
Protecting Server 2003 against Ransomware 2 79
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
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…

732 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