Solved

batch script for creating folders and moving files

Posted on 2011-02-23
1
275 Views
Last Modified: 2012-05-11
I have files with a name in the format of -> 12345678_ordrsp.test
What I am trying to do is take the first 8 characters "12345678" which would be custno
and the characters 10-15 "ordrsp" which would be messagetype.

I would then copy the file to one folder and then move the original file to another.

My code so far:

setlocal EnableDelayedExpansion

dir /b c:\edi_out\*.xml > filelist.txt

pushd c:\edi_out

REM append .xml to all files
rename *_*.* *.*.xml

for /F "tokens=*" %%F in (filelist.txt) do (
   set str=%%F
   set custno=%str:~0,8%
   set messagetype=%str:~10,15%

   echo %custno
xcopy "%%~F" "C:\backup\%custno%\%messagetype%"
move "%%~F" "C:\%custno%\%messagetype%"
)
popd
pause
0
Comment
Question by:failed
1 Comment
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 34966509
I think this does what you are looking for:

@echo off
for %%A in ("c:\edi_out\*.xml) do (
  for /F "tokens=1-2 delims=._" %%B in ("%%A") do (
    copy "%%A" "c:\backup\%%B\%%C\%%~nxA.xml"
    move "%%A" "c:\%%B\%%C\%%~nxA.xml"
  )
)

Open in new window

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

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

23 Experts available now in Live!

Get 1:1 Help Now