Question

I am trying to create a batch script on windows 7 to do the following:

I have a folder which another program dumps files to. All the files have numbers in their name along with other identifying information. I would like to have a batch script search for all the digits in the file name and then create a folder (folder name is just the digits) and move all files that contain those digits to the folder. It must be applicable to numerous users in my office.

currently i have a very bad primitive system running which doesn't account for mistakes:

@echo off 

setlocal enabledelayedexpansion

pushd "%USERPROFILE%\Desktop\RawFiles"

for %%a in (*) do (

  set fldr=%%~na

  set fldr=!fldr:~0,5!

  md "!fldr!"

  move "%%a" "!fldr!"

)

popd


 if not exist "%USERPROFILE%\Dropbox\agents" mkdir "%USERPROFILE%\Dropbox\agents"

 SET "src_folder=%USERPROFILE%\Desktop\RawFiles"

 SET "tar_folder=%USERPROFILE%\Dropbox\agents"

 for /f %%a IN ('dir "%src_folder%" /b') do move /-y %src_folder%\%%a %tar_folder%


exit

the files in the "rawfiles" folder are as follows:

12345 - tech pack.pdf

12345.pdf

12345-artwork.AI

"#12345- artwork.AI"

What i created only works for perfect instances where the numbers come first in files and are exactly 5. Unfortunately there is a lot of human error here so this solution is not viable. an example of a mistake is an accidental 6 digit number or even putting the "#" sign before the numbers

I also would like to be able to override the files in the destination folder by running this twice but am getting "access denied".

Please help!

thanks

Était-ce utile?

La solution

The Batch file below extract the digits from the file names as requested (maximum 6 groups of digits separated by other characters, this may be modified), so you may manipulate they in any way you wish; the ECHO commands are just examples. Note that the space must be the last character in eliminate variable.

@echo off
setlocal EnableDelayedExpansion

set "eliminate=#-abcdefghijklmnopqrstuvwxyz "

for %%a in (*.*) do (
   for /F "tokens=1-6 delims=%eliminate%" %%b in ("%%~Na") do set folder=%%b%%c%%d%%e%%f%%g
   if not exist "!folder!" ECHO md "!folder!"
   ECHO move "%%a" "!folder!"
)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top