Run batch script on all files in a folder - windows

I have a folder receiving 'calculations' files daily. Sometimes a few files arrive. I want to execute a batch script on all of them in a loop. Files may have different name but may contain three extensions only: .ta, .tb, .tc.
I am trying to run a batch script on all of them, rename the files so that they contain a timestamp and move to archive folder. I have tried multiple combinations of the below but I can't figure it out:
cd C:\test\files
FOR %%x IN (*.ta, *.tb, *.tca) DO (
call C:\test\batches\combine.bat
move %%x "C:\test\archive\calculations.txt-%date:/=-%_%time::=-%"
)
In the above case, combine.bat is not executed properly and the file is not moved. However, when I run it as follows it is launching combine.bat and moving the file. But if I have two files, combine.bat is executed on both but the second one is not moved to archive folder:
FOR /R C:\test\files %%x IN (*.ta, *.tb, *.tca) DO (
call C:\test\batches\combine.bat
move %%x "C:\test\archive\calculations.txt-%date:/=-%_%time::=-%"
)
The problem is that there are other folders under C:\test\files and I want the 'combine.bat' to run for this specific path only and not for any sub-folders. Is it failing because I have changed directory in the beginning to C:\test\files and the loop is then calling to another directory?
Appreciate you help.

Related

Move Batch files to folder with similar name

I am trying to move batch files to their respective folder, they have similar names expect the batch files I want to move have "Build" on the end. For example:
Folder name: wld-vine-tstrm
Bat file name: wld-vine-tstrmBuild.bat
In the same parent directory, I also have batch files with identical names to the folder, I do not want these to move, only the ones with "Build" at the end of the file names.
setlocal enabledelayedexpansion
set folderpath=E:\Build\ModelBatches\world-wild\actor50
for %%f in (%folderpath%\*.bat) do (
set "foldername=%%~nf"
move "%%f" "!foldername:~0,-5!"
)
This is very close to the result I am looking for; it moves the "Build" batches to the respective folder, but it also removes the last 5 characters from the batches I dont want to move.
The below batch files are now missing the last 5 chars, "tstrm"
To summarize, There are two batch files for each folder in a directory, I only want to move the one ending "Build" to its folder.
You should use this:
setlocal enabledelayedexpansion
set folderpath=E:\Build\ModelBatches\world-wild\actor50
for %%f in (%folderpath%\*build.bat) do (
set "foldername=%%~nf"
move "%%f" "!foldername:~0,-5!"
)
The wildcard *build.bat finds all batch files ending with "build" but wildcard "*.bat" shows them all.

Script to generate checksums for new files only?

I’ve created (well, lifted) a script for powershell to monitor a folder for new files, when new files are added it runs the following batch file to generate checksums:
for %%a in (*.txt) do md5sum "%%a" >> "%%a.md5"
Files are regularly being added to the folder so I only want the script to run on the new files. At the moment it runs from scratch each time generating checksums for the same file over and over. I tried an IF NOT EXIST statement but this only checks the folder for a single instant:
if not exist "*.txt.md5" (for %%a in (*.txt) do md5sum "%%a" >> "%%a.md5")
I presume I need to scan the folder, generate a list of files and then skip over the ones that have checksums? This is where it goes a bit over my head - can anyone advise?
One thing to note - it’s important each file has a separate checksum (e.g. File.txt - File.txt.md5) opposed to generated in a single list, as the files go their separate ways.
Thank you!

Run subdirectories of a folder through an exe. Batch file needed

So I have a problem: I usually need to run a folder through an exe, but now I need to run all the folders in a main directory. They don't have any folder depth so one subfolder has no other subfolders. Essentially I drag and drop a folder onto my exe and I get a file from it, I need a batch script that runs all the folders in my directory individually through the exe, as when I run multiple folders through the exe, I still get a single file.
I found this code and this works for all files with the suffix .emb
#echo off
for /r %%i in (*.emb) do "embpack_v2.exe" "%%i" ALL
but I need it to run folders, not files ending in .emb
try with
for /r /d %%i in (*) do "embpack_v2.exe" "%%i" ALL

run texconv windows batch file, recursively and output to the same folder as source?

im running texconv-r.bat in this directory:
I've included "/r" so that it runs recursively on folder "sub" too
for /r %%f in (*.jpg, *.png, *.bmp) do ( texconv -pow2 -f BC1_UNORM %%f )
however, the output places the contents of the sub directory, in the batch file directory:
rather, I would like it outputted to the same directory - the sub directory
I cant figure out what i need to change in the batch file to make this work? ive tried what's recommended in this post but i cant get it to work
Thanks in advance

How to execute a windows batch command recursively?

For example, you have a rename command in a batch file, and you want to execute that file on the current directory and all sub-directories.
Suppose your batch is named something like myrename.cmd, then you can easily do the following:
call myrename.cmd
for /r /d %%x in (*) do (
pushd "%%x"
call myrename.cmd
popd
)
The first line will run it for the current directory, the for loop will iterate recursively (/r) over all directories (/d) and execute the part in the parentheses. What we do inside them is change the directory to the one we're currently iterating over with pushd—which has the nice property that you can undo that directory change with popd—and then run the command, which then will be run in the directory we just switched to.
This assumes that the batch lies somewhere in the path. If it doesn't and just happens to lie where the batch file above lies, then you can use
"%~dp0myrename.cmd"

Resources