Batch File how to run all files in folder - batch-file

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.

Related

How can I create a folder with the same name as the file names in that folder?

What I want to accomplish can be done with for %%i in (*) do md "%%~ni" however this only works if my batch file is in the same folder as the files I want to process. I want to run a batch file from a another folder.
This is what I have tried so far and it's not working. It is still creating the folders in the same folder I run the batch file.
for %%i in ("D:\test1\*") do md "D:\test2\" "%%~ni"
What am I doing wrong? I have not written a script before.
You need to concatenate the path with the name
like this
for %%i in ("D:\test1\*") do md "D:\test2\%%~ni"

Renaming Multiple Files in Subdirectories

Long story short, I accidentally wiped my HDD the other day while trying to install a new SSD. I used a great program to undelete my partition; however, all the files were given an .efs extension. I am currently using:
:begin
RENAME *.extension.efs *.
I have tons of lines written for each file type, and this command works flawlessly. The only problem is that I have to manually paste this .bat into every folder and execute it in order for it to work.
Is there a way I can make it so when I run this .bat, it will go through all folders and subdirectories from a central directory? I'm anal about organization so all my music, albums, videos, TV shows, etc., are all in separate folders and it would take quite some time for me to run my original .bat from each.
Any help is appreciated!!
for /r /d %n in (.) do pushd "%n"&call "fullpathtoyourniftybatchfile"&popd
from the prompt will traverse the entire tree from wherever your current directory is.
You could also place your batch into any directory on your path and execute
for /r /d %n in (.) do pushd "%n"&call "yourniftybatchfilename"&popd
since windows searches the path for any executable it can't find in the current directory.
Here is a script that will rename files with extension efs in the current directory and all subfolders starting at the current directory. %%i is replaced by the full path to a folder or subfolders. Line 2 is needed because the for loop only does folders and subfolders.
#echo off
ren *.efs *.
for /f "tokens=* usebackq" %%i in (`dir/b/ad/s`) do (
cd %%i
ren *.efs *.
)

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

Batch script to delete files

I have a batch script as follows.
D:
del "D:\TEST\TEST1\Archive\*.TSV"
del "D:\TEST\TEST1\Archive\*.TXT"
del "D:\TEST\TEST2\Archive\*.TSV"
del "D:\TEST\TEST2\Archive\*.TXT"
del "D:\TEST\TEST 100%\Archive\*.TSV"
del "D:\TEST\TEST 100%\Archive\*.TXT"
The above code deletes all the ".txt" and ".tsv" files from all the folders except from the folder TEST 100%. For deleting the files from TEST 100% i am getting the error as The Path could not be found. I guess the % symbol in the folder name creates the issue.
Can anyone guide me to resolve the issue and to delete the files from the folder TEST 100%?
You need to escape the % with another...
del "D:\TEST\TEST 100%%\Archive*.TXT"
There's multiple ways of doing things in batch, so if escaping with a double percent %% isn't working for you, then you could try something like this:
set olddir=%CD%
cd /d "path of folder"
del "file name/ or *.txt etc..."
cd /d "%olddir%"
How this works:
set olddir=%CD% sets the variable "olddir" or any other variable name you like to the directory
your batch file was launched from.
cd /d "path of folder" changes the current directory the batch will be looking at. keep the
quotations and change path of folder to which ever path you aiming for.
del "file name/ or *.txt etc..." will delete the file in the current directory your batch is looking at, just don't add a directory path before the file name and just have the full file name or, to delete multiple files with the same extension with *.txt or whatever extension you need.
cd /d "%olddir%" takes the variable saved with your old path and goes back to the directory you started the batch with, its not important if you don't want the batch going back to its previous directory path, and like stated before the variable name can be changed to whatever you wish by changing the set olddir=%CD% line.
Lets say you saved your software onto your desktop.
if you want to remove an entire folder like an uninstaller program you could use this.
cd C:\Users\User\Detsktop\
rd /s /q SOFTWARE
this will delete the entire folder called software and all of its files and subfolders
Make Sure You Delete The Correct Folder Cause This Does Not Have A Yes / No Option
Consider that the files you need to delete have an extension txt and is located in the location D:\My Folder, then you could use the below code inside the bat file.
cd "D:\My Folder"
DEL *.txt
in batch code your path should not contain any Space so pls change your folder name from "TEST 100%" to "TEST_100%" and your new code will be
del "D:\TEST\TEST_100%\Archive*.TXT"
hope this will resolve your problem

.bat program to search and find a particular .exe file and then run that file once

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

Resources