带变量的批处理脚本文件传输 [英] Batch Script File Transfer with Variables

查看:77
本文介绍了带变量的批处理脚本文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您抽出宝贵的时间来帮助我解决我要解决的问题.我已经写了一些批处理文件,但是我没有太多经验,所以这对我来说有点困难.我正在尝试将pdf文件从特定位置传输到另一个位置的单个文件夹.每个文件将包含此格式 "GOOGLE EARTH_2018-08-07_5485A635.pdf",并基于"GOOGLE EARTH_"将其转移到名为"GOOGLE EARTH_Google Corporation"的合法文件夹中.因此,基于文件的初始部分,将其传输到以指定文件名开头的文件夹.到目前为止,这是我目前无法使用的内容.

Thank you for taking your time to help me with the problem I'm trying to solve. I have written some of the batch file but I don't have much experience so this a bit difficult for me. I'm trying to transfer pdf files from a specific location to individual folders in another location. Each file will contain this format "GOOGLE EARTH_2018-08-07_5485A635.pdf" and based on "GOOGLE EARTH_" it will transfer it to it's rightful folder named as such "GOOGLE EARTH_Google Corporation". So based on the initial part of the file, transfer it to folder that begins with specified file name. This is what I have so far but doesn't work.

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Files"
SET "destdir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Transfer"
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\*.pdf" '
 ) DO (
 FOR /f "tokens=1delims=_-" %%b IN ("%%a") ) DO (
  FOR /f "delims=" %%d IN (
  'dir /b /ad "%destdir%\*%%b*" '
  ) DO (
   MOVE "%%a" "%sourcedir%\%%d\"
 )
)

GOTO :EOF

推荐答案

在我的回答中,我已对代码进行了对齐以提高可读性,以向您显示每个FOR命令的右括号如何对齐.我更改了MOVE命令以使用源目录变量,因为第一个FOR变量仅包含文件名.还更改了第一个IN子句以迭代PDF文件.

In my answer I have aligned your code for extreme readability to show you how the closing parentheses lines up with each FOR command. I changed the MOVE command to use the source directory variable because the first FOR variable only holds the file name. Also changed the first IN clause to iterate PDF files.

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Files"
SET "destdir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Transfer"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*.pdf" ') DO (
    FOR /f "tokens=1 delims=_-" %%b IN ("%%a") DO (
        FOR /f "delims=" %%d IN ('dir /b /ad "%destdir%\*%%b*" ') DO (
            MOVE "%sourcedir%\%%a" "%destdir%\%%d\"
        )
    )
)
GOTO :EOF

这篇关于带变量的批处理脚本文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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