简体   繁体   中英

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. 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')

There is no need to have a script for this. 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

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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