Solved

batch script on windows

Posted on 2011-02-21
10
319 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I guess it is not common knowledge to most Wintel engineers/administrators: If you have an SNMP-based monitoring system in your environment (and it's common to have SNMP or Syslog) it's reasonably easy to enable monitoring of the Windows Event logs,…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

18 Experts available now in Live!

Get 1:1 Help Now