1

I am trying to come up with a batch file to copy files based on their names. The filenames are in form aaa_xx-XX-ccc.txt where aaa and ccc do not matter (variable length), but xx are 2 character codes. I would like to copy the files into their respective xx folders and these folders would also be created by the script.

Here I found something that almost suit my needs: Batch file to copy files based on characters in filename

@echo off setlocal enableextensions enabledelayedexpansion for %%x in (*.txt) do ( set "filename=%%x" set "folder=!filename:~1,2!" if not exist !folder! mkdir !folder! copy "%%x" !folder! )

But I would like to be looking for the xx not only by its position in the filename, but by the specific character (underscore) and position in the filename.

Is there any way to do it?

Update - example: I have the following files in a single folder:

My-Project_ar-SA-2016114-12h33m10s.txt

My-Project_cs-CZ-2016114-12h33m8s.text

My-Project_da-DK-2016114-12h33m10s.txt

The "template" for the filenames is "[project-name]_[language-CODE]-[dateAndTime].txt

And I want to copy the first file into a different location\ar folder (for example "C:\Output\ar"), the second file into "C:\Output\cs" etc.

Community
  • 1
  • 1
user5908315
  • 13
  • 1
  • 4

2 Answers2

0

According that all .txtfiles in the directory you're running the BAT are to be copied and the template is stricly what you described.

Here is an idea to do it :

@echo off

for /f "tokens=1,2,3* delims=_-" %%a in ('dir *.txt /b/a-d') do echo copy "%%a%%b%%c%%d" "c:\output\%%c"

Remove the echo before the copy if the Output is OK

SachaDee
  • 9,245
  • 3
  • 23
  • 33
0
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
  'dir /b /a-d "%sourcedir%\*_*-*-*.txt" '
  ) DO (
 FOR /f "tokens=1*delims=_" %%c IN ("%%a") DO (
  FOR /f "tokens=1,2*delims=-" %%q IN ("%%d") DO (
   ECHO(COPY "%sourcedir%\%%a" "%destdir%\%%q\"
  )
 )
)

GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances.

The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)

It isn't clear whether you want the destination directory to be xx or XX. This is set up for xx for XX use %%r in place of %%q in the copy line.

Read the directory for files only using the mask _--.txt into %%a

Remove the leading characters up to the underscore by tokenising using _ and selecting %%d, the trailing portion

re-tokenise %%d on -. first token to %%q, second to %%r

Mix and match to create the copy command.

Note that your second filename, ...CZ... is a .text filename, not a .txt

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hello, is there any solution if I want both the "xx-XX" for the destination directory? I tried to modify the copy command to: COPY "%sourcedir%\%%a" "%destdir%\%%q\-\%%r\" but it keeps giving me wrong paths. – user5908315 Feb 16 '16 at 20:20