简体   繁体   中英

Retrieving variable value from filename with a batch file

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM