0

Is there a way with a batch file to instantly MOVE a file when it's downloaded into Directory A into Directory B based on a variable in the file's name?

I have a file naming convention that looks like this: Photo-87654321-1.jpeg

The 87654321 part is the variable. Now, with if statements and such, I can locate the directory, or if it doesn't exist, create the directory and then place the image in there. The problems I'm having is: a) copying that variable string from the file name, b) running this script every time a file is moved into Directory A.

Plummer
  • 6,522
  • 13
  • 47
  • 75
  • I've seen a way to do this in [BASH](http://stackoverflow.com/questions/428109/extract-substring-in-bash), please tell me Windows can do it. – Plummer Dec 03 '12 at 16:04

1 Answers1

1

You haven't given enough details about what you want to do with the file when you find the variable number, so I can only improvise.

This script will get the variable name from the files in C:\DirectoryA and then move them into a folder with that name.

:LOOP
for /f "tokens=2 delims=-" %%a in ('dir /b /a-d "C:\DirectoryA"') do (
md "%%~na"
move "%%a" "%%~na"
)
goto :LOOP

This should give you enough details to tweak to your needs, but if you need anything more specific please provide more details.

Note: Given that you want to move files as soon as they are put in DirectoryA, this is on an infinite loop, so you may want to watch your CPU.

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Does it need to loop for every file in that directory? File dumps occur once an hour. I figure with a scheduled task, modifying this to run for all the files currently in the directory should get it done. Any references to the commands you used? Thanks! – Plummer Dec 03 '12 at 16:14
  • Ok, if you are using a scheduled task just remove the `:LOOP` from the top and the `goto :LOOP` at the bottom and it will only run once. – Bali C Dec 03 '12 at 16:17
  • It doesn't have to, I just searched all the files as default. What files do you want it to search? – Bali C Dec 03 '12 at 16:21
  • No, it's all the files. You assumption was correct. Now, the root folder is in a different location. Would I just add that directory string before `%%~na? in the md and move lines? – Plummer Dec 03 '12 at 16:23
  • Yep. If you want to search DirectoryA which is under the root folder of DirectoryRoot, you would use `"DirectoryRoot\%%~na"` for `md` and `move`. – Bali C Dec 03 '12 at 16:30
  • Also, missing a `:` in directory file attributes. – Plummer Dec 03 '12 at 16:37
  • What do you mean? Where is a `:` supposed to be? – Bali C Dec 03 '12 at 16:58
  • `/a:-d` for the file attribute in the command. They were also in the wrong order. `'dir /a:-d /b "C:\DirectoryA"'` Just incase anyone else comes across it and doesn't know the syntax. – Plummer Dec 03 '12 at 17:01
  • It works without as well, at least when I was testing, and I'm pretty sure the order doesn't matter, but whatever works! – Bali C Dec 03 '12 at 18:43