I have several bat files on network. I like to create one bat so I can run all one time. Also I like to use *.bat in new bat so I don't need copy each bat files.
Using the FOR command you can locate files with a wildcard in the name and then run all of them in sequence.
FOR /F "usebackq " %%F in (`dir "%~dp0filename*.BAT`) DO Call "%~dp0%%~F"
This code will list all files that start with "filename" and end in ".bat" and then rin them. This use of the FOR command will work in folders with spaces in the name. It will even run from a UNC path (one that start with \ and is located on a network share).
Related
I'm looking to get a batch file to apply to all sub directories.
I have a number of folders. Each folder will contain file pairs
xxx1.mp3 and xxx1.cdg,
xxx2.mp3 and xxx2.cdg,
etc.
I have a 7zip batch file that will look for file pairs and create a single zip file xxx1.zip, xxx2.zip and delete the (now) redundant cdg/mp3 files.
However, this will only work if the bat file is run in each individual folder. What I'm really looking for is a switch to add to the bat file that if I run in the root directory, will run through all sub directories also.
The code I currently run is:
FOR %F IN (*.cdg) DO "C:\Program Files\7-Zip\7Z.exe" a "%~nF.zip" "%~nF.cdg" "%~nF.mp3" -sdel
Any help?
FOR /R %%F IN (*.cdg) DO IF EXIST "%%~dpnF.mp3" pushd "%%~dpF"&ECHO("C:\Program Files\7-Zip\7Z.exe" a "%%~nF.zip" "%%~nF.cdg" "%%~nF.mp3" -sdel&POPD
Note that this is a batch line - to execute from the prompt, reduce each %% in metavariable to % (but it's a whole lot easier to put it into a batch by cut-and-paste;saves all those typos...)
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, delete the string ECHO( which appears before your command to actually execute the commands.
Using for /r will iterate through the filenames matching the supplied mask (it's possible to also add a starting directory - see for /?|more from the prompt.)
Since we know the selected full filename exists, first check that the paired file exists, then switch to the required directory, archive, and switch back.
I'm working on a start script, and I have a text file output.txt, containing paths to programs, like:
C:/program1.exe
C:/abab/program2.exe
How do I now run the programs contained in the text file via a batch script?
for /f "usebackq delims=" %A in ("C:\output.txt") Do Start "" "%A"
Start starts programs without waiting for them to exit (so new window) and first set of quotes is the window title. UseBackq is needed to use quotes around output.txt. See For /? and my answer here for how to start programs Trouble with renaming folders and sub folders using Batch. In a batch use %%A and %A when typing interactively (I don't use batch files, I keep stuff in one text file and paste parts into a command prompt window,which is interactive).
If they were to be run sequentially then
Rename the file to batch and run it. Open in notepad and search for / and replace with \ as that is the windows standard (although autocorrect will fix it without telling you but can sometimes cause problems).
I am trying to find a batch file command that will allow me to run all .lnk or .html files with in a folder. I tried using the wildcards (*.lnk) but this wouldn't work. Any ideas?
Based upon further comments, this should help - you don't need recursion to process a single folder, and this will handle long filenames.
#echo off
for %%v in ("C:\Users\username\Desktop\Test\*.lnk") do start "" "%%~v"
It will open every .lnk file in the folder.
You can use this:
for /r %%v in (*.lnk) do start %%v
Note:
Don't put spaces on shortcut filenames.
I'm trying to use the FOR /f command to calculate the filesize of multiple files with the same extension. I have this so far:
FOR /F "usebackq" %%A IN ('C:\Users\%username%\*.bat') DO set size=%%~zA
If I have 3 batch files under the desktop, and 2 under my documents, I want the batch file to get the size of each and display all 5 together. I have the command working perfectly if I use just a single directory with batch files, but all I need is for it to include subdirectories. The current line I have above displays ECHO is off because there are NO batch files under the path C:\Users\%username%. I have batch files on my desktop, and I need it to include it since it's a subdirectory. How can I get this command to include subdirectories?
FOR /R
Loop through files (Recurse subfolders)
Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command
Key
drive:path : The folder tree where the files are located.
set : A set of one or more files. Wildcards must be used.
If (set) is a period character (.) then FOR will
loop through every folder.
command : The command(s) to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)
This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.
If the [drive:]path are not specified they will default to the current drive:path.
Unlike some other variants of the FOR command you must include a wildcard (either * or ?) in the 'set' to get consistent results returned. In many cases you can work around this by adding a single character wildcard e.g. if you are looping through multiple folders to find the exact filename myfile.txt you could instead specify myfile.t?t
http://ss64.com/nt/for_r.html
dir is far easier.
dir "%userprofile%\*.bat" /a /s
or just the sizes
dir "%userprofile%\*.bat" /a /s|findstr /c:"File(s)"
I want to create a .bat file that will help me to find/search a particular .exe file in folders in a particular location and then run that .exe file.
e.g
search file in particular location e.g."\G:\data" and in this prime location there is many folders and in that folders many sub folders are there. and the .exe file is available in one of these sub-folder and the location I do not know. only I know the name of the .exe file.
bat file need to search that file and then run that one.
below two are not able to find and run the file.
1st program
pushd G:\data-a\test
FOR /F "delims=" %F IN ('dir /S /b autorun.exe') DO SET ExePath="%F"
%ExePath%e
2nd program
for /r G:\data-a\test %a in (autorun.exe) do "%a"
from the above two different program I put it in to notepad for creating two different .bat file and executed one after anther file. but that was unable to locate and run autorun.exe file.
just for example my autorun.exe file exact locations are
G:\data-a\test\test1\t1 and G:\data-a\test\test1\t1
but in G:\data-a\test location there may be several subfolders.
so to run autorun.exe one by one that to find the file in subfolder is difficult. I want to give the path G:\data-a\test in program and all the files will be found in subfolder and then run automatically.
Three liner (use %%F if wrapping inside a batch file).
pushd G:\data
FOR /F "delims=" %F IN ('dir /S /b something.exe') DO SET ExePath="%F"
%ExePath%
or the one liner from the command prompt:
for /r g:\data %a in (filename.exe) do "%a"
used the quotes for %a in case the calling directory path has spaces in it