使用批处理文件从带日期的文件夹中复制所选文件 [英] Copying selected files from dated folder using batch file

查看:128
本文介绍了使用批处理文件从带日期的文件夹中复制所选文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带日期的文件夹(例如20180601、20180602等)的共享目录(源文件夹称为F :)中有一组文件. 我的目标驱动器位于本地驱动器中(目标位置为C:).

我想将选定的文件从源复制到目标.

例如

F:\Source\20180601\abc1.csv to C:\Destination\20180601\abc1.csv
F:\Source\20180601\abc2.csv to C:\Destination\20180601\abc2.csv

....

现在,我要在目标位置手动创建文件夹,并手动复制所选文件,但是我无法对已过时的文件夹进行此操作.

感谢您的帮助.预先感谢.

解决方案

这个问题尚不清楚,但我还是尝试回答.

我想将选定的文件从源复制到目标.

我假定文件是在Windows资源管理器中选择的,目标是将具有完整路径的所选文件从驱动器F:复制到驱动器C:,但用Destination替换第一个目录Source./p>

可以使用以下批处理文件完成此操作:

@echo off
for %%A in (%*) do for /F "tokens=1* delims=\" %%B in ("%%~pA") do %SystemRoot%\System32\xcopy.exe "%%~A" "C:\Destination\%%C" /C /F /G /H /K /R /Y
pause

批处理文件可以存储在任何目录中.为了与Windows资源管理器中的选定文件一起运行,必须在用户帐户的SendTo文件夹中创建一个快捷方式文件(* .lnk).

SendTo文件夹在Windows XP上为%USERPROFILE%\SendTo文件夹,在Windows Vista及更高版本的Windows版本上为%APPDATA%\Microsoft\Windows\SendTo文件夹(由 ASCII表.因此,循环变量C保留当前文件的目录路径,而不包含F:\Source\,并以反斜杠结尾,这对于下一个命令 XCOPY 很重要.

注意: tokens=之后的数字确定源文件路径中忽略的目录级别数.

命令 XCOPY 用于将完全限定文件名保留在循环变量A中的文件复制到文件夹C:\Destination,并创建目录树作为保留在循环变量如有必要.如果目标参数以反斜杠结尾,则 XCOPY 总是将目标参数解释为目录.这就是为什么循环变量C保留以反斜杠结尾的路径如此重要的原因.

在复制所有文件之后,由于 PAUSE 命令,cmd.exe暂停了批处理文件的执行,以便您可以验证所有操作是否按预期进行.

请注意,批处理文件不能用于一次执行复制成千上万个文件.命令行的长度受Windows限制.因此,不可能从Windows资源管理器传递给成批文件数千个完全合格的文件名.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • call /? ...解释%*.
  • echo /?
  • for /?
  • xcopy /?

I have a set of files in a shared directory (source folder say F:) in the dated folder (e.g. 20180601, 20180602 etc). I have the destination folder which is in my local drive (destination say C:).

I want to copy the selected files from source to destination.

e.g.

F:\Source\20180601\abc1.csv to C:\Destination\20180601\abc1.csv
F:\Source\20180601\abc2.csv to C:\Destination\20180601\abc2.csv

....

Now I am manually creating the folder in destination and copying the selected files manually, I am not able to iterate this for dated folders.

Any help is appreciated. Thanks in advance.

解决方案

The question is very unclear, but I try nevertheless to answer it.

I want to copy the selected files from source to destination.

I assume the files are selected in Windows Explorer and the goal is to copy the selected files with full path from drive F: to drive C:, but with replacing the first directory Source by Destination.

This could be done using the following batch file:

@echo off
for %%A in (%*) do for /F "tokens=1* delims=\" %%B in ("%%~pA") do %SystemRoot%\System32\xcopy.exe "%%~A" "C:\Destination\%%C" /C /F /G /H /K /R /Y
pause

The batch file can be stored in any directory. For running it with the selected files in Windows Explorer it is necessary to create a shortcut file (*.lnk) in SendTo folder of your user account.

The SendTo folder is on Windows XP the folder %USERPROFILE%\SendTo and on Windows Vista and later Windows versions the folder %APPDATA%\Microsoft\Windows\SendTo (as confirmed by Stephan).

To create the shortcut file, click with secondary (right) mouse button on the batch file and click in opened context menu in submenu Send to on menu item Desktop (create shortcut).

Switch to your Windows desktop and click with secondary (right) mouse button on the just created shortcut and left click in opened context menu on last item Properties.

First append on Target the string  %* (space, percent sign, asterisk) which is important to run the batch file later with full qualified file names of all selected files passed as arguments to the batch file by Windows Explorer. Second write in Comment a comment useful for you to know later what this shortcut does on using it. You can also click on button Change Icon and select a better icon from %SystemRoot%\System32\shell32.dll as the one Windows has automatically chosen for the batch file shortcut. Then close the Properties with button OK.

Click again with secondary (right) mouse button on the just created and modified shortcut and click with primary (left) mouse button on menu item Rename. Rename the shortcut file to whatever you want like Copy Selected Files.

Now the shortcut file on Windows desktop is ready for being moved to the SendTo folder. So select the shortcut file with a single click on it with primary (left) mouse button , press Ctrl+X to cut it, open in Windows Explorer the SendTo folder of your user account and paste the shortcut file with Ctrl+V into that folder.

The batch file is ready for usage now from within Windows Explorer. You can select one or more files (not folders) in F:\Source, click with secondary (right) mouse button on one of the selected files to open the context menu and left click in submenu Send To on Copy Selected Files or whatever name you have given the shortcut file.

The batch file runs a FOR loop processing all file names passed to the batch file referenced with %*. Each file name is assigned one after the other to loop variable A.

The path of the current file without drive letter F: is processed by a second FOR which splits the path string into two substrings using backslash as delimiter. The first substring is Source which is assigned to specified loop variable B which is not further used. Everything else of path without drive letter and colon after \Source\ is assigned to next loop variable C according to ASCII table. So loop variable C holds the directory path of current file without F:\Source\ and ending with a backslash which is important for next command XCOPY.

Note: The number after tokens= determines the number of directory levels ignored from source file path.

The command XCOPY is used to copy the file of which full qualified file name is hold in loop variable A to folder C:\Destination with creating the directory tree as hold in loop variable C if that is necessary. XCOPY interprets destination argument always as directory if the destination argument ends with a backslash. That's the reason why it is so important here that loop variable C holds the path ending with a backslash.

After all files are copied, the execution of batch file processing is halted by cmd.exe because of command PAUSE so that you can verify if everything worked as expected.

Please note that the batch file can't be used to copy thousands of files with one single execution. The length of the command line is limited by Windows. So it is not possible to pass from Windows Explorer to batch file thousands of full qualified file names.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %*.
  • echo /?
  • for /?
  • xcopy /?

这篇关于使用批处理文件从带日期的文件夹中复制所选文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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