简体   繁体   English

从Windows命令行自动复制文件

[英]Automate file copying from Windows command line

Say you have 100 directories and for each directory you have a file named .pdf stored somewhere else. 假设您有100个目录,对于每个目录,都有一个名为.pdf的文件存储在其他位置。 If you want to move/copy each file into the directory with the same name, can this be done on the Windows command line? 如果要将每个文件移动/复制到相同名称的目录中,可以在Windows命令行上执行此操作吗?

This is a batch script that probably does what you want: 这是一个批处理脚本,可能会满足您的要求:

setlocal
set target_dir=D:\
set source_dir=C:\WINDOWS

for %%i in (%source_dir%\*.pdf) do move %%i %target_dir%\%%~ni.%%~xi

endlocal

You can do it using the FOR command . 您可以使用FOR命令执行此操作 Something in the line of: 符合:

for /f %%f in ('dir /s /b c:\source\*.pdf') do copy "%%f" c:\target

If you have a list of the file names w/ full path in a text file, say files.txt, you can also do 如果在文本文件中有完整路径的文件名列表,例如files.txt,您也可以

for /f %%f in (files.txt) do copy "%%f" c:\target

From the command line: 在命令行中:

for /f %f in ('dir /s /b mypath\*.pdf') do @copy %~nxf myotherpath

As it's on a command line and not in a batch file you only need %, not %%. 因为它是在命令行上,而不是在批处理文件中,所以您只需要%,而不是%%。

dir /s /b is recursive and bare. dir / s / b是递归且裸露的。 (see dir /?) (请参阅目录/?)

The @ before copy stops the echo of each copy command. 复制前的@会停止每个复制命令的回显。 You can echo them if you like, up to you. 您可以根据自己的喜好来回应它们。

%~nxf gets the name and extension of %f. %〜nxf获得%f的名称和扩展名。 (see call /?) (请参阅电话/?)

您将需要编写脚本来遍历每个文件(及其路径),提取文件名-.pdf,然后将文件移至同名目录

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

相关问题 脚本自动复制文件? - script to automate copying a file? 使用批处理文件自动化Windows命令行实用程序 - 在实用程序启动后将键击发送到std输入 - automate a windows command line utility with a batch file - send keystrokes to std input after utility starts Windows命令行:从.cmd文件自动键入命令 - Windows command line: automatically type in a command from .cmd file 如何在不复制的情况下从 Windows 命令行合并两个目录或移动替换? - How do you merge two directories, or move with replace, from the windows command line without copying? Windows:自动执行几个不同应用程序的命令行顺序 - Windows: Automate sequence of command line for several different applications 使用 subprocess.run 自动化命令行应用程序(Windows 10) - using subprocess.run to automate a command line application (windows 10) 从Windows命令行加载带有参数的序言文件 - Loading a prolog file with arguments from windows command line 在Windows 7中从命令行打开目录中的最新文件 - Opening the most recent file in a directory from the command line in Windows 7 如何在Windows OS 7中从命令行运行因素文件? - how to run factor file from command line in windows os 7? Windows:如何从外部文件读取命令行选项? - Windows: How to read command-line options from external file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM