0

I'm looking for a batch file to accomplish the following tasks:

  1. Batch file to Look at the folder and sub folders of a location (for example: \server1\main\ and its sub folders) and copy the files based on the first 3 letter of the files' names to multiple folders corresponding to their names.

  2. After the copy is finished I would like to move the original files/folder to an archive directory.

The file format is as follows: AAA_xxx_xxx.jpg - the first 3 letters of the file name is what we care about.

Example:

  • Files \\server1\main\AAA\AAA_xxx_xxx.jpg will get copied to \\server2\files\AAA\
  • Files \\server1\main\BBB\BBB_xxx_xxx.jpg will get copied to \\server2\files\BBB\
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • You don't specify what OS you're using -- it sounds like Windows, but that's not clear -- which is important. Also, have you tried anything yet? – elixenide Dec 06 '13 at 01:05
  • P.S. How do you determine whether to copy to server 1 or server 2? – elixenide Dec 06 '13 at 01:07
  • Say `%filename%` equals `AAA_xxx_xxx.jpg` - `%filename:~0,3%` will therefore equal `AAA`. Look into [string manipulation](http://www.dostips.com/DtTipsStringManipulation.php). I also recommend that you provide insight into how you've already gone about trying to achieve this. – unclemeat Dec 06 '13 at 01:14
  • 1
    @EdCottrell: "Batch file" is a term commonly applied to Windows batch files (formerly MS-DOS batch files). – Andriy M Dec 06 '13 at 12:14
  • Batch file string manipulation is indeed what this question is essentially about. Apart from @unclemeat's useful link, you can also search this very site for ready solutions on the subject. Here's but one resolved question to start with: [Batch file to copy files based on part of filename from one server to another](http://stackoverflow.com/questions/11693446/batch-file-to-copy-files-based-on-part-of-filename-from-one-server-to-another) – Andriy M Dec 06 '13 at 13:24
  • @AndriyM: I know what a batch file is. It's not 100% clear from the question that the OP really means a Windows batch file, however. Many novices in *nix systems (myself included, once upon a time) don't know the correct terminology or use of forward and back slashes. – elixenide Dec 06 '13 at 14:17
  • Sorry for the confusion, but yes this batch file is for windows. link was provided can do the copy what do you guys suggest to do for doing the archive after the files have been copied. i would like to move the sub-folder including its files to be moved to a archive folder. – user3072553 Dec 07 '13 at 00:43

1 Answers1

0
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\one"
SET "sourcedir2=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
SET "targetdir=U:\targetdir"
FOR %%d IN ("%sourcedir%" "%sourcedir2%") DO (
 FOR /f "tokens=1*delims=_" %%a IN (
   'dir /b /a-d "%%~d\*_*.jpg" '
   ) DO (
  ECHO(MD "%destdir%\%%a\"
  ECHO(COPY /y "%%~d\%%a_%%b" "%destdir%\%%a\"
  ECHO(MOVE "%%~d\%%a_%%b" "%targetdir%\"
 )
)

GOTO :EOF

You would need to change the setting of sourcedir, sourcedir2, destdir and targetdir to suit your circumstances.

The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)

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

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.

Oh - having done the work, I see I'm a victim of a reformatter and this question has whiskers. Oh well - maybe it'll help someone else.

Magoo
  • 77,302
  • 8
  • 62
  • 84