Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

batch script for creating folders and moving files

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
failed
Asked:
failed
1 Solution
 
Bill PrewCommented:
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now