简体   繁体   English

使用批处理文件从文件名中检索变量值

[英]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? 使用批处理文件时,是否存在基于文件名中的变量将文件下载到目录A中并立即将其MOVE目录B中的方法?

I have a file naming convention that looks like this: Photo-87654321-1.jpeg 我有一个看起来像这样的文件命名约定: Photo-87654321-1.jpeg

The 87654321 part is the variable. 87654321部分是变量。 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. 现在,使用if语句等,我可以找到目录,或者如果目录不存在,则创建目录,然后将映像放在其中。 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 . 我遇到的问题是:a)从文件名复制该变量字符串,b)每次将文件移动到目录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. 该脚本将从C:\\DirectoryA的文件中获取变量名称,然后将其移动到具有该名称的文件夹中。

: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. 注意:鉴于您要立即将文件放入DirectoryA ,因此这是一个无限循环,因此您可能需要监视CPU。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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