Creating Bat file to execute a command on all files in folder - windows

How to create a bat file that will execute a single command on all files with a particular extension.
I look for something like this which is there is linux for windows batch files
command *.extension
or even a way to loop through the file with extension would do.

If your command changes the filename and uses the same extension then this solution has a bug, and some files are processed more than once. There are ways around that.
#echo off
for %%a in (*.ext) do (
echo "%%a"
)

Related

Moving files with .bat script on windows 10

Recently I wanted to make .bat script that would move .avi files from subfolders in specified directory to another directory.
e.g.
H:\MAINDIRECTORY\dir1\avi1.avi
H:\MAINDIRECTORY\dir2\avi2.avi
H:\MAINDIRECTORY\dir3\avi3.avi
....
To one directory called e.g.
H:\Movies
I've made script which looks something like this
#echo off
move H:\Pobrane\*\*.avi H:\Filmy
But when I'm tryin to execute it I got something like this:
the file name, directory name or volume lable syntax is incorrect
Please help me find a way out from this situation.
As move does not support wild cards you can try with for /r
#echo off
for /r "H:\Pobrane\" %%# in (*.avi) do (
move /y "%%~f#" "H:\Filmy"
)

Batch file to create a text file with the same name as every .zip file in a directory

I'm not very familiar with windows batch scripts except very simple applications. I'm currently trying to figure out how to run a batch file in a local directory, scan the directory for each *.zip file and then create a corresponding .txt file (in the same local directory). This will run indefinitely at intervals of say... 5 seconds. Any clues to help? Would it be easier to write a Java program or something?
You can use the FOR command to iterate through files in a directory. The TYPE command creates an empty file.
#echo off
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
I got it:
#echo off
:loop
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
timeout /t 300
goto loop

Batch file "for" command

Need to be able to "schedule" a command line action using windows scheduler.
This is my command:
for /r C:\Users\bob\Downloads\Done\ %f in (*) do #move "%f" C:\Users\bob\Downloads\Done\
It flattens all files into the main folder.
The "for" command does not work as in a scheduler but is working in cmd.exe
I need a batch file or other code so that I can use the scheduler.
I read these, but I'm just not that cool:
How to copy files from folder tree dropping all the folders with Robocopy?
How does "FOR" work in cmd batch file?
Take your above code and save it in a .bat file. The only change you need to make, is that percents (%) need to be doubled-up when run inside a batch file.
for /r C:\Users\bob\Downloads\Done\ %%f in (*) do (
#move "%%f" C:\Users\bob\Downloads\Done\
)

Win 7: CMD batch file for creating directories based on filenames

I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.

Batch script to rename and move files

I have a radio management system which is capable of running batch scripts after shows.
I'm looking for a batch script file that renames any file in the directory like so:
1.mp3 --> [Replay]1.mp3
And then move the file from folder a to folder b.
Any thoughts on how do i go about creating such script in a syntax level?
Here you go
ren *.mp3 [Replay]*.*
There are many ways to do what you request, this script does that by searching all mp3 files in the current folder and move them to folderb specifying a new name.
#ECHO off
FOR %%i IN (*.mp3) DO (
MOVE "%%i" "folderb\[Replay]%%i"
)

Resources