Running many commands via batch, running into annoying issue - windows

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?

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

Related

How to batch rename files in windows removing everything from them but set of listed strings?

I have went through a lot of guides for it, but havent found a way to do something similar to lets say turn all files in folder, like these files:
djhwu4s_cat_ruhg29.png
397y_dog_j0929_ej93.png
8yhh_owl.png
into these:
_cat.png
_dog.png
_owl.png
So basically removing everything from file names but a list of predefined strings i am searching for. In example above i would define list as "_cat", "_dog", "_owl". I know that each file will have only one of these variables, and there will be only one file with each of them in folder.
Will appreciate any tips on how to achieve that. Thanks in advance!
edit:
Here is what i came up with (with stuff i can understund) and what seems to be working fine now.
#echo off
setlocal enabledelayedexpansion
set v1=_cat-cat
set v2=_cat-owl
set v3=_cat
set v4=_dog
set v5=_owl
set v6=_horse
FOR /L %%a IN (1,1,6) DO (
rem echo %%a
rem echo !v%%a!
FOR /f %%f in ('dir /b /a:-D *!v%%a!.*') DO (
REN %%f !v%%a!.*
)
FOR /f %%f in ('dir /b /a:-D *!v%%a!_*.*') DO (
REN %%f !v%%a!.*
)
)
rem using two passes of this simpler code i can grasp and understund with dot and with underscore
rem after constructed variables value i make sure cat-cat is not recognised as and renamed to cat
rem no matter if im getting file with that variable as the last string before extension or another underscore
rem Gonna test it in combat now
For some reason this stuff doesnt work with files containing spaces and characters like:
"ab’c efg_dog.png"
FOR /L %%a IN (1,1,36) DO (
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
REN "%%f" "!v%%a!.*"
)
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
REN "%%f" "!v%%a!.*"
)
)
After further testing i have realised the problem starts with the %%f, and not the REN function as i thought. echo %%f before ren gives just the first part of the name to the first space, hence the REN function cant find the file. In case of "ab’c efg_dog.png" after finding the file with dir, the %%f becomes just "ab’c".
edit: After more tests and experiments and adding those "delims" to the code, the echo now shows proper, full names to be renamed, but it replaces that weird ’ character with ' for the REN command and thats why it still cant find the file to rename.
FOR /L %%a IN (1,1,36) DO (
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
)
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"
FOR /f "delims=" %%e IN ('dir /b /a-d "*_*" 2^nul ^|findstr /v /b "_"') DO (
FOR /f "tokens=2delims=_." %%y IN ("%%e") DO ECHO REN "%%e" "_%%y%%~xe"
)
POPD
GOTO :EOF
For lack of examples, here's a start.
Always verify against a test directory before applying to real data.
Process the list of filenames that match *_*; find those names that do not start _, pick the second string between the delimiters _ and . and rename using that string (in %%y), prefixed by _ and appended with the original extension.
The ren command is simply echoed to the screen for verification. When happy, remove the echo before the ren to actually execute the rename.

How can I make my BATCH file affect subfolders?

I have a BATCH file which deletes every other JPG file. I am looking to make it so that when it is run, it affect all subfolders as well.
I am not a coder by all means, so apologies if this is a rookie question!
Below is my current code:
#echooff
setlocal
for /r %%D in (.) do (
set "del="
for /f %%F in ('dir /b "%%D\*.jpg"') do if defined del (
del "%%D\%%F"
set "del="
) else set "del=1"
)
PAUSE
The /S switch in the dir makes you go through subdirectories, as you can see here:
dir /?
...
/S Displays files in specified directory and all subdirectories.
...
So I'd advise you to replace:
dir /b "%%D\*.jpg"
by:
dir /S /b "%%D\*.jpg"
Good luck
Based upon your clarifications, is this what you're trying to achieve:
#SetLocal EnableExtensions DisableDelayedExpansion
#For /F "EOL=?Delims=" %%G In ('Dir /B/S/A:D 2^>NUL') Do #(Set "_="
For /F "EOL=?Delims=" %%H In ('Dir /B/A:-D/O:N "%%G\*.jpg" 2^>NUL')Do #(
If Not Defined _ (Set "_=T")Else Del /A/F "%%G\%%H"&Set "_="))

windows batch script: Remove all non pdf files in directory

I am trying to write a batch script, which removes all non pdf files from a directory.
The directory name is OUTPUT/
this what i got
FOR /f %%f IN (.\OUTPUT) DO
SET fileName=%%f
IF NOT "!fileName:~-3!"=="pdf" (
DEL !fileName!
)
Although your question is simply that you're not using the correct syntax with your for command, there are other remarks I noted, which I decided would be better as an answer.
The most obvious one is that there is that you're setting a variable for no reason, and then having to use it with delayed expansion.
To do it as you were, it should look more like this:
#Echo Off
SetLocal DisableDelayedExpansion
For %%G In (Output\*) Do (
Set "fileName=%%G"
SetLocal EnableDelayedExpansion
If /I Not "!fileName:~-4!"==".pdf" (
Del /F "!fileName!"
)
EndLocal
)
These entire batch files should do the same thing.
Standard for loop:
#For %%G In (Output\*)Do #If /I Not "%~xG"==".pdf" Del /F "%%G"
Using the for /f variant:
#For /F "Delims=" %%G In ('Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"')Do #Del /F "Output\%%G"
or over multiple lines, if you find it easier to read:
#Echo Off
For /F "Delims=" %%G In (
'Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"'
) Do (
Del /F "Output\%%G"
)
Please remember that almost every windows cli command has built-in help, and the vast majority accept the /? option, e.g. for /?, del /?. Additionally you can use the help command, e.g. help dir, help findstr.

Batch script to move files that contain certain string

Hi guys just got the following code script running to search in a directory for files that contain the string "ABC" and move them to the directory at the end.
for /f "eol=: delims=" %%F in ('dir /b^|find "ABC"') do move /Y "%%F" "C:\DESTINATION_DIRECTORY"
Was wondering how to modify this to not have to be run from the input directory, i.e to add a SOURCE_DIRECTORY variable so I can run this script from elsewhere but have it parse thru the SOURCE_DIRECTORY.
Thanks for any help.
#echo off
set "root=c:\st"
pushd %root% && (
for %%# in ("*ABC*") do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"
)
popd
or
for /f "tokens=* delims=" %%# in ('dir /b "%root%\*ABC*"') do echo move /Y "%root%\%%~nx#" "C:\DESTINATION_DIRECTORY"
for recursive search:
for /f "tokens=* delims=" %%# in ('dir /b /s "%root%\*ABC*"') do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"

Deleting all files except the most 3 recent in a folder and sub folders

To clean up our back-ups folder I have written a short batch file that will be run Via task scheduler to periodically clear all files except the 3 most recent copies.
Currently I have the following
for /f "skip=3 eol=: delims=" %%F in ('dir /b /o-d *.sqb ^| findstr /r /c:"LOG_.*"') do #del "%%F"
This will work for the files inside the folder with the batch file, however I would like to run it from a higher level to check all the backups, I have tried the /s command but these only leaves the 3 most recent out of ALL folders where as I need to keep the 3 most recent files in EVERY folder and subfolder
Loop recursively by directories first:
for /r /d %%a in (*) do (
for /f "skip=3 eol=* delims=" %%b in ('dir /b /a-d /o-d "%%a\*LOG*.sqb"') do (
del "%%~fb"
)
) 2>nul

Resources