如何批复制文件的基于preFIXES列表(TXT)上 [英] How to batch copy files based on a list (txt) of PREFIXES

查看:92
本文介绍了如何批复制文件的基于preFIXES列表(TXT)上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件名包含有点标准化prefixes。一般而言,格式为 YYYY-99999-XX

文件名的例子:

  2015-12345-NY-0康尼岛
2015-12345-NY-1康尼岛
2015-54321-NY康尼岛

如你所见,可以有包含相同字符 1 直通多个文件ñ。我想通过搜索包含在.txt文件列表prefixes从文件夹中复制文件。

基本上,我想细化/扩大的所示的过程批处理:复制文件列表(TXT),使文件LIST.TXT - 我已经更改为 preFIX -List.txt - 仅包含的文件的文件名prefixes进行复制。我不得不改变在 XCOPY 开关设置为 / K ,以复制任何文件。

此外,我想错误重定向到 ERRORS.TXT

很高兴有将是包括在搜索子文件夹。

使用上面我的文件名的例子:

2015年将复制3个文件。结果
2015-12345-NY 将复制2个文件。结果
2015-12345-NY-1 将复制1个文件。

下面是我的.bat文件

 集SRC_FOLDER = D:\\ JAR \\源
设置dst_folder = D:\\ JAR \\目标
FOR / F令牌= *%% i的(文件LIST.TXT)DO(
    XCOPY / K%SRC_FOLDER%\\ %% i的%dst_folder%


渔业部的解决方案,这正是我问。现在我想通过更改目标目录名和放大器扩大一点;添加日期时间preFIX。

我有2个问题结果
     1.如何获得周一= 08(而不是8月)?结果
     2.什么是语法使用变量MKDIR?

下面是我使用的编码(从<修改href=\"http://stackoverflow.com/questions/10945572/windows-batch-formatted-date-into-variable\">Windows批:格式化的日期为可变)。

 关闭@echo
 SETLOCAL
 FOR / F跳过= 8 =令牌2,3,4,5,6,7,8 delims =%% D IN('的Robocopy / L * \\ \\ / NS / NC / NDL / NFL / NP / NJH / XF * / XD *')做(
 设置陶氏= %% D
 设置月= %% E
 一套天= %% F
 设置HH = %% G
 设置MM = %% H
 设置SS = %%我
 集的一年= %% J
 SETDESTINATION = %%Ĵ%%Ë%%˚F%%摹%% ^ h %%我拔牙
) 本周回声日:道琼斯%%
 本月回声日:当天%%
 回声月:%月%
 回声时间:HH%%
 回声分钟:MM%%
 回声秒:SS%%
 回声年:%,比去年%
 回声目的地:DESTINATION%% ENDLOCAL MKDIR {DESTINATION%%}


解决方案

下面是我的建议与意见这个小批处理文件:

 关闭@echo
REM定义源和目标目录。
设置SRC_FOLDER = D:\\ JAR \\源
设置dst_folder = D:\\ JAR \\目标REM从previous运行删除错误日志文件。
德尔ERRORS.TXT 2 - ; NULREM复制所有文件中的所有子目录匹配
REM在preFIX列表的文本文件的任何preFIX字符串。
FOR / Fdelims =%% i的(preFIX-LIST.TXT)做(
    XCOPY%SRC_FOLDER%\\ %%我*%dst_folder%/ C / H / I / K / Q / R / S / Y&GT; NUL 2 - ;&GT; ERRORS.TXT
)REM删除ERRORS.TXT如果是因为没有错误的空。
为%% f由于(ERRORS.TXT)做,如果%%〜ZF == 0德尔ERRORS.TXT

但我怀疑你是错误信息的 XCOPY 错误信息是没有印刷复制的文件来安慰和他们都重定向到文本文件中,也确实没有用的。

真的很开心

有关用过的命令的详细信息,打开命令提示符窗口中运行有以下命令和读取所有输出帮助页面。


  • DEL /?

  • 为/?

  • 如果/?

  • 设置/?

  • XCOPY /?


扩展


  1. 请源文件夹通过第一个参数上执行的批处理文件传递的变量。结果
    如果批处理文件不带参数运行显示错误消息。结果
    显示错误消息,如果指定的源文件夹不存在。


  2. preFIX YYYY-MM-DD _ 为目标目录的独立本地日期格式。


以上的批处理文件的这款改装版包括两个扩展:

 关闭@echo
REM检查批处理文件的第一个参数。
如果%〜1==转到NoSourceFolder
如果不存在%〜1转到SourceNotExist
如果不存在preFIX-LIST.TXT后藤没有prefixListSETLOCAL
REM基于参数定义源目录。
设置SRC_FOLDER =%〜1REM获取的语言无关的格式本地日期和时间。
FOR / F令牌= 1,2 delims ==%% i的('了Wmic.exe OS获取LocalDateTime /值2 ^&GT; NUL')做(
    如果我%%。==。LocalDateTime。设置= LOCALDATE %% J

REM定义格式YYYY-MM-DD当地日期。
设置= LOCALDATE%LOCALDATE:0,4〜% - %LOCALDATE:〜4.2% - %LOCALDATE:6,2〜%REM定义与日期preFIX目标目录。
设置dst_folder = D:\\ JAR \\%%LOCALDATE _destinationREM从previous运行删除错误日志文件。
德尔ERRORS.TXT 2 - ; NULREM复制所有文件中的所有子目录匹配
REM在preFIX列表的文本文件的任何preFIX字符串。
FOR / Fdelims =%% i的(preFIX-LIST.TXT)做(
    XCOPY%SRC_FOLDER%\\ %%我*%dst_folder%/ C / H / I / K / Q / R / S / Y&GT; NUL 2 - ;&GT; ERRORS.TXT
)REM删除ERRORS.TXT如果是因为没有错误的空。
为%% f由于(ERRORS.TXT)做,如果%%〜ZF == 0德尔ERRORS.TXT
ENDLOCAL
GOTO:EOF:NoSourceFolder
回声%〜NX0:缺少源文件夹的名称作为第一个参数。
转到HaltOnError:SourceNotExist
回声%〜NX0:没有文件夹%〜1。
转到HaltOnError:没有prefixList
回声%〜NX0:没有文件%CD%\\ preFIX-LIST.TXT。:HaltOnError
回声。
暂停

由于去的答案周杰伦上的如何获得在Windows命令行当前日期时间,以合适的格式使用的文件名?

有关%〜1 %〜NX0 在命令提示符窗口中运行呼叫/?和读取所有的输出帮助页面,并阅读也是什么%〜DP0的意思,以及如何它的工作原理?

My filenames contain somewhat standardized prefixes. Generally, the format is yyyy-99999-xx.

Filename examples:

2015-12345-NY-0 Coney Island
2015-12345-NY-1 Coney Island
2015-54321-NY Coney Island

As you can see, there can be multiple files containing identical characters 1 thru n. I would like to copy files from a folder by searching for prefixes contained in a .txt file list.

Basically, I would like to "refine/expand" the process shown in Batch: Copy a list (txt) of files so that file-list.txt - I've changed to Prefix-List.txt - contains only the filename "prefixes" of the files to copy. I had to change the xcopy switch to /K in order to copy any files.

Additionally, I would like to redirect errors to Errors.txt

"Nice to have" would be including subfolders in the search.

Using my filename examples above:

2015 would copy 3 files.
2015-12345-NY would copy 2 files.
2015-12345-NY-1 would copy 1 file.

Here's my .bat file

set src_folder=d:\JAR\source
set dst_folder=d:\JAR\destination
for /f "tokens=*" %%i in (File-list.txt) DO (
    xcopy /K "%src_folder%\%%i" "%dst_folder%"
)


Mofi's solution is exactly what I asked for. Now I'd like to expand a little by changing the destination directory name & adding a date-time prefix.

I have 2 questions
1. How to get Mon=08 (instead of Aug)?
2. What is syntax for MKDIR using a variable?

Here's the coding I'm using (modified from Windows batch: formatted date into variable ).

 @echo off
 setlocal 
 for /f "skip=8 tokens=2,3,4,5,6,7,8 delims=: " %%D in ('robocopy /l * \ \  /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
 set "dow=%%D"
 set "month=%%E"
 set "day=%%F"
 set "HH=%%G"
 set "MM=%%H"
 set "SS=%%I"
 set "year=%%J"
 SET "DESTINATION=%%J%%E%%F%%G%%H%%I-EXTRACTION"
)

 echo Day of the week: %dow%
 echo Day of the month : %day%
 echo Month : %month%
 echo hour : %HH%
 echo minutes : %MM%
 echo seconds : %SS%
 echo year : %year%
 echo DESTINATION : %DESTINATION%

 endlocal

 MKDIR {"%DESTINATION%"}

解决方案

Here is my suggestion for this little batch file with comments:

@echo off
rem Define source and destination directory.
set "src_folder=d:\JAR\source"
set "dst_folder=d:\JAR\destination"

rem Delete error log file from a previous run.
del Errors.txt 2>nul

rem Copy all files in all subdirectories matching
rem any prefix string in prefix list text file.
for /F "delims=" %%i in (Prefix-List.txt) do (
    xcopy "%src_folder%\%%i*" "%dst_folder%" /C /H /I /K /Q /R /S /Y >nul 2>>Errors.txt
)

rem Delete Errors.txt if it is empty because of no error.
for %%F in (Errors.txt) do if %%~zF == 0 del Errors.txt

But I doubt you are really happy with error information as xcopy error messages are really not useful without printing copied files to console and redirecting them all into the text file, too.

For details on the used command, open a command prompt window, run there the following commands and read all output help pages.

  • del /?
  • for /?
  • if /?
  • set /?
  • xcopy /?

Extensions:

  1. Make the source folder a variable passed via first parameter to batch file on execution.
    Display an error message if batch file is executed without a parameter.
    Display an error message if specified source folder does not exist.

  2. Prefix yyyy-mm-dd_ for destination directory independent on local date format.

This modified version of above batch file includes the two extensions:

@echo off
rem Check first parameter of batch file.
if "%~1" == "" goto NoSourceFolder
if not exist "%~1" goto SourceNotExist
if not exist Prefix-List.txt goto NoPrefixList

setlocal
rem Define source directory based on parameter.
set "src_folder=%~1"

rem Get local date and time in a language independent format.
for /F "tokens=1,2 delims==" %%i in ('wmic.exe os get LocalDateTime /VALUE 2^>NUL') do (
    if ".%%i."==".LocalDateTime." set "LocalDate=%%j"
)
rem Define local date in format yyyy-mm-dd.
set "LocalDate=%LocalDate:~0,4%-%LocalDate:~4,2%-%LocalDate:~6,2%"

rem Define destination directory with date prefix.
set "dst_folder=d:\JAR\%LocalDate%_destination"

rem Delete error log file from a previous run.
del Errors.txt 2>nul

rem Copy all files in all subdirectories matching
rem any prefix string in prefix list text file.
for /F "delims=" %%i in (Prefix-List.txt) do (
    xcopy "%src_folder%\%%i*" "%dst_folder%" /C /H /I /K /Q /R /S /Y >nul 2>>Errors.txt
)

rem Delete Errors.txt if it is empty because of no error.
for %%F in (Errors.txt) do if %%~zF == 0 del Errors.txt
endlocal
goto :EOF

:NoSourceFolder
echo %~nx0: Missing source folder name as first parameter.
goto HaltOnError

:SourceNotExist
echo %~nx0: There is no folder "%~1".
goto HaltOnError

:NoPrefixList
echo %~nx0: There is no file "%CD%\Prefix-List.txt".

:HaltOnError
echo.
pause

Thanks goes to Jay for answer on How to get current datetime on Windows command line, in a suitable format for using in a filename?

For details on %~1 and %~nx0 run in a command prompt window call /? and read all the output help pages, and read also What does %~dp0 mean, and how does it work?.

这篇关于如何批复制文件的基于preFIXES列表(TXT)上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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