批处理文件,该文件搜索与本地文件同名的文件夹,并将该文件移动到找到的文件夹中不起作用 [英] Batch file that searches for a folder with the same name as a local file and moves said file to the located folder not working

查看:56
本文介绍了批处理文件,该文件搜索与本地文件同名的文件夹,并将该文件移动到找到的文件夹中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个批处理文件,该批处理文件将在与该批处理文件相同的目录中使用pdf文件,并输出文件名(不带扩展名).我使用以下代码来完成此任务:

So I am trying to create a batch file that will take a pdf file in the same directory as the batch file and output the file name (sans extension). I used this code to accomplish this:

@echo off

for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do set "name=%%~nG"

这很好.下一步是搜索另一个目录,并在搜索到的目录中找到一个目录,该目录的名称与上述代码的输出相匹配(存储在%name%变量中).这是我尝试过的:

This works fine. The next step is to search another directory and find a directory within the searched directory whose name matches the output of the above code (stored in the %name% variable). Here's what I tried:

dir "P:\Accounting\Acc Pay" | find %name% | set "loc=%%~dp"

以上代码的目标是仅查找与原始pdf文件名称相同的目录,然后将输出的驱动器和路径设置为变量%loc%.我认为这是我搞砸的地方.

The goal of the above code was to find only the directories that had the same name as the original pdf file and then set the drive and path of the output to a variable %loc%. I think this is where I messed up.

一旦文件夹的路径设置为%loc%,我就应该以这一行结束:

Once the path to the folder is set to %loc%, I then am supposed to finish with this line:

move .\*.pdf %loc%

这将把带有批处理文件的目录中的所有pdf文件(一次在目录中只有一个)都带走,并将其移至当前存储在%loc%变量中的路径.

This would take all the pdf files (there will only be one in the directory at once) in the directory with the batch file and move it to the path currently stored in the %loc% variable.

总的代码如下:

@echo off

for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do set "name=%%~nG"

for /r %%A in ('dir "P:\Accounting\Acc Pay" | find %name%') do set "loc=%%~dpA"

move .\*.pdf %loc%

但是,代码似乎将pdf文件移动到了已经存在的位置(即包含批处理文件的文件夹).我认为%loc%变量无法正常工作.任何帮助表示赞赏.

However, the code seems to move the pdf file into the same location it was already in (ie the folder with the batch file). I assume the %loc% variable is not working properly. Any help much appreciated.

推荐答案

就像@Magoo说的那样( ^ | ).并且您肯定要为 dir 命令添加以下开关:用于裸格式"的/b ;(仅名称)和属性目录"的/ad 仅返回文件夹名称. find 需要用引号引起来,并且为了安全起见,还应该用 move 命令的目标位置加引号.您的 find 可能会受益于/i ,使其不区分大小写.

Like @Magoo said (^|). And you surely want to add the following switches for the dir command: /b for "bare format" (name only) and /ad for "Attribute Directory" to return folder names only. find needs its's argument quoted, and for safety, the destination for the move command should also be quoted. Your find could benefit from /i to make it case-insensitive.

我个人会使用嵌套循环来避免创建超变量:

I personally would do it with nested loops to avoid creating superflouos variables:

@echo off
for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do (
  for /r %%A in ('dir /b /ad "P:\Accounting\Acc Pay" ^| find /i "%%~nG"') do (
    move "%%G" "%%~dpA"
  )
)

奖金:如果有多个 .pdf 文件(可能在周末"或假期"之后),则可以一次性正确处理所有文件.

Bonus: should there be more than one .pdf file (maybe after the Weekend or Holidays), this would process all of them correctly in one go.

根据您的命名结构,考虑将 find/i"%%〜nG" 替换为 findstr/iblc:"%%〜nG" (请参见 findstr/?找出开关的含义)

Depending on your naming structures, consider replacing find /i "%%~nG" with findstr /iblc:"%%~nG" (see findstr /?to find out what the switches mean)

(为避免混淆,请注意: findstr 是唯一支持将交换机串联到一起的命令(据我所知)./iblc /i/b/l/c )

(Note to prevent confusion: findstr is the only command (as far as I'm aware) that supports concatenating switches into one. /iblc is the same as /i /b /l /c)

这篇关于批处理文件,该文件搜索与本地文件同名的文件夹,并将该文件移动到找到的文件夹中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆