Home > OS >  Batch file to copy contents from sub-folder to parent only if there's one file and it's th
Batch file to copy contents from sub-folder to parent only if there's one file and it's th

Time:12-09

I've been fiddling with this for several hours now and I've gotten it almost there but I'm still lacking bits here... I do NOT have access to powershell.exe; I can only use Windows cmd.exe here.

Basically, what I want to do is traverse only the top level folders in the current director and if:

  1. There's only one file in that subfolder and
  2. The file name without the extension matches the name of the folder it is in

If both of those conditions are true, then copy the file one level up and move on to the next folder in the target directory.

Here's what I have so far.

for /D %%f in (*) do (
    set %count&=(dir /b /a-d | find /v /c "?")
    echo %count%
    if %count%=="1" (
        if %~nf=="Something"(
            echo %%f
            echo copy * .. /-Y  
        )
     )
)

Could someone help me fill in the missing bits here?

So far, the value of count is always 0 and I haven't gotten the part that compares the filename to its folder (the current folder in the iteration)

CodePudding user response:

@ECHO OFF
SETLOCAL

for /D %%e in (*) do (
 rem directory name in %%e
 for /f %%c in ('dir /b /a-d "           
  • Related