Solved Backup creating different folders

September 17, 2011 at 01:12:08
Specs: Linux/VMWare

I need to create a .bat script for a backup copying files from one single folder to another but I need to create individual folders to put all the files that contains the same 3 digits number in front of the file name.

Files name: 001_xxxx.gif ------> to be copied to the folder 001
001.xxxxxxxxxx.xlsm ------> to be copied to the folder 001
002.xxxxxxxxxx.xlsm ------> to be copied to the folder 002
100_xxxx_xxxxx.gif ------> to be copied to the folder 100

I have several different files extension with the same number and need to put all of them together in the same folder. During the backup if the folder already exists, copy to this one; if it doesn't exist yet, creates one.

Does someone have any idea of how to do this?

Thanks in advance!! :D

See More: Backup creating different folders

Report •

September 17, 2011 at 08:39:58
✔ Best Answer
@echo off & setlocal EnableDelayedExpansion

cd /D Source_Folder
set Target=Destination_Folder

for %%j in (*) do (
  set name=%%j
  set name=!name:~0,3!
  if not exist "%Target%.\!name!" md "%Target%.\!name!"
  copy "%%j" "%Target%.\!name!" > nul

Report •

September 17, 2011 at 22:57:49
Thank you very much IVO, always saving my life (and making it easier)!! :D

Report •
Related Solutions

Ask Question