I need help writing a script in cmd

I am in a folder that has subfolders in it, you have to search these subfolders (do not go deeper) and copy files whose name ends with "_0001.png" to the folder !! _ TEMP (this folder is in the place where the program starts)

Can you help me?

This not work:

for /R "%folder%" %%G in (*_0001.png) do copy "%%G" "%folder%\!!_TEMP\"
| improve this question | | | | |
pushd "%folder%"
for /D %%D in (*) do copy "%%D\*_0001.png" "!!_TEMP"
| improve this answer | | | | |

CMD is not my strength, so I'll offer my Powershell solution.

$content = Get-ChildItem .\Desktop\localtmp -Depth 1
foreach($file in $content){
    if($file.Name -like "*_0001.png"){
    $path = $file.DirectoryName + "\" + $file.Name
        Copy-Item $path -Destination .\Desktop\anotherlocaltmp

you just need to add your paths

Hope it helps!

| improve this answer | | | | |
  • thx but i create .bat file so this not work for me :( – Alan May 28 '19 at 9:41
  • if that task is the only job of the script, take mine put it as a .ps1 and sit back and relax ^^ or is it larger? – A guest who's called Redd May 28 '19 at 9:44
  • unfortunately a lot larger :( but thank you anyway – Alan May 28 '19 at 9:54
  • I'm sorry i couldn't help you further, if you have any problems with Powershell feel free to come back at me ;) Good luck! – A guest who's called Redd May 28 '19 at 10:07
  • 1
    @Alan if you really need to call it from a batch file then use powershell -command your_command or powershell -file scriptfile. But if you're new to cmd then do yourself a favor and learn powershell instead. It's much more powerful and doesn't contain legacy issues – phuclv May 28 '19 at 11:07

This should work for you:

@echo off

set "dir=C:\Your\Root\Folder"
set "tem=%dir%\!!_Temp"

for /d %%A in (*) do (
    for %%B in (%%A\*_0001.png) do (
        if not exist "%tem%\%%~nxB" copy /y "%%B" "%tem%\" 

To make things cleaner and easier to change, we set two variables - one for your root folder (dir) and one for the !!_Temp folder (tem) inside your root; this also assumes based on your question that you're running the .bat file from the root folder - if this isn't the case we can edit the path in the first for-loop.

The first loop has for /d which looks only at top-level folders in your current directory, and for each iteration of the loop those top-level folders are set as parameter %%A. The next loop looks only at files matching the criteria we set - which is anything in %%A that ends in _0001.png - and sets each of those as parameter %%B. The command in this final loop is if not exist "%tem%\%%~nxB" copy /y "%%B" "%tem%\" - we include the if not exist so it doesn't run on files that have already been copied to !!_Temp. Based on your question I'm not sure if you'd ever run into multiple files in multiple subfolders that have the exact same name, but if that is a potential case we can work around that.

| improve this answer | | | | |

This one line for loop should work. It has the same variables as the ones you are using.

for /f "delims==" %%G in ('dir "%folder%\*_0001.png" /s /b') do copy "%%G" "%folder%\!!_TEMP\"
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.