Search code examples
windowsbatch-filermdir

Running many commands via batch, running into annoying issue


What I am trying to do is automate moving downloads, deleting unneeded files and then running a program through command line as well as who knows what else I think up of going through this quest. Here I have the beginning of my first .bat file:

move.bat:

@echo off
for /f "tokens=*" %%f in ('dir /a:-D /s /b') do move "%%f" .
for /f "tokens=*" %%f in ('dir /a:D /s /b') do rd "%%f"
del "C:\Downloads\*.jpg"
del "C:\Downloads\*.png"
del "C:\Downloads\*.gif"
del "C:\Downloads\*.txt"
del "C:\Downloads\*.nfo"
move "C:\Downloads\*.mp4" "C:\Movies\"
cd "C:\program files\filebot\"
filebot -rename c:\movies
copy "E:\Documents\Tweaks&commands\folderize.bat" "C:\Movies"
cd "C:\Movies\"
folderize.bat
ping 192.0.2.2 -n 1 -w 4000 > nul
rmdir /S /Q "C:\movies\folderize"

Now, everything works just fine up to running folderize.bat.

Folderize.bat:

@echo off
setlocal enabledelayedexpansion
for %%a in (*.eng*) do (
  set file=%%a
  ren "!file!" "!file:_=!"
)
@echo off
for %%a in (*.*) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)

Folderize.bat does exactly what it's supposed to do, but if I run that it will include itself, creating a folder named folderize and then moving itself into there. Ok, not a problem, I'll just do a rmdir and all is well.

That doesn't work.

In Move.bat I originally tried without the ping command line. I added that line as I thought it just needed a couple seconds before attempting delete (honestly just a stab in the dark). That was a no go as well.

I opened a cmd prompt and ran it manually to see what would happen. After folderize is initialized, it is then moved into it's own folder prompting that the batch file could not be found and terminates.

Is there a way that I can edit folderize.bat to exclude .bat files from being placed in a folder or really just exclude itself?


Solution

  • Seems like it's moving everything in your folderize.bat batch file. Maybe you should put a filter on it like so:

    @echo off
    setlocal enabledelayedexpansion
    for %%a in (*.eng*) do (
      set file=%%a
      ren "!file!" "!file:_=!"
    )
    @echo off
    for /f "tokens=1* delims=" %%a in ('dir /b /o:-n ^| findstr /iv "folderize.bat"') do (
    md "%%~na" 2>nul
    move "%%a" "%%~na"
    )
    

    Also, you use use the call command in your move.bat, like so:

    call folderize.bat