简体   繁体   English

Windows脚本删除文件并将其复制到多个目录

[英]Windows script to delete and copy file to multiple directories

Scenario: 场景:

There are about 150 directories all with very similar path names, but one folder in that path differs between each entry. 大约有150个目录,它们的路径名非常相似,但是每个条目之间该路径中的一个文件夹都不同。 Examples below 下面的例子

d:\TSProfiles\coetzeed.TEST.V2\Desktop\
d:\TSProfiles\ronaldb.TEST.V2\Desktop\
d:\TSProfiles\mcondaldc.TEST.V2\Desktop\

I want to do the following on all those directories, but somehow account for those different folder names in each entry, without having to add all those directories into the script. 我想对所有这些目录执行以下操作,但是以某种方式在每个条目中考虑了这些不同的文件夹名称,而不必将所有这些目录添加到脚本中。

del d:\TSProfiles\coetzeed.TEST.V2\Desktop\accounts.exe   (deletes the old accounts.exe)
Copy d:\newdesktop\accounts.exe  d:\TSProfiles\coetzeed.TEST.V2\Desktop\  (copies the new accounts.exe to the destination folder

)

On the commandprompt in one single line do this: 单行的命令提示符上执行以下操作:

for /f "tokens=1" %a in ('dir d:\TSProfiles\* /ad /b ') do 
    echo copy /Y d:\newdesktop\accounts.exe  "d:\tsprofiles\%a\Desktop"

I put in a echo so you can check if the command will work, remove it if you're happy. 我输入了回声,以便您可以检查该命令是否有效,如果感到满意,请将其删除。

the command dir /ad /b basically spits out just the foldernames (/ad) without any humbug ( the /b stands or 'bare') 命令dir / ad / b基本上只吐出文件夹名称(/ ad),而没有任何混淆(/ b代表“ bare”或“ bare”)

There is no need to have a script for this. 无需为此script It just runs fine directly form the command prompt. 直接从命令提示符运行就可以了。 If you want to use this in a script don't forget to replace the single % with a double %% for varaible a , so %a becomes %%a 如果你想使用这个脚本中,不要忘记与varaible双%%来代替单一% a ,那么%a%%a

@echo off
pushd "d:\TSProfiles\"
for /f "delims=" %%F in ('dir /b /s /a:-d "*accounts.exe"') do (
 del "%%~F" /F /Q
 copy "d:\newdesktop\accounts.exe" "%%~dpF" /Y

)
popd

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM