How to execute any .exe by using a batch file? - windows

I know that I can start an exe by doing:
start "" /b filename.exe
But that requires me to know the name of filename.exe, how could I do that for any general file ending with .exe? I tried the obvious wildcard implementation:
start "" /b *.exe
Windows, however, gives me an error saying it cannot find "*.exe" file.

if you plan to run inside a batch file you can do in this way:
for %%i in (*.exe) do start "" /b "%%i"
if you want to skip a particular file to be executed:
for %%i in (*.exe) do if not "%%~nxi" == "blabla.exe" start "" /b "%%i"
if is necessary to check also the subfolders add the /r parameter:
for /r %%i in (*.exe) do start "" /b "%%i"

From cmd run this to the folder that has all the exe you wish to run:
for %x in (*.exe) do ( start "" /b "%x" )

Hoep it helps
for /f "delims=" %%a in ('dir /b /s "*.exe"') do (
start %%a
)
You should first use dir command to find all exe files, and then execute it.

In a bat file add this line
FOR /F "tokens=4" %%G IN ('dir /A-D /-C ^| find ".exe"') DO start "" /b %%G
This execute every .exe file in your current directory. same as
*.exe
would have done if * were supported on batch.
If you want to execute it directly from a command line window, just do
FOR /F "tokens=4" %G IN ('dir /A-D /-C ^| find ".exe"') DO start "" /b %G

Don't blame their codes for space issue. You should know how to use double quotation marks.
for /f "delims=" %%a in ('dir /b /s *.exe') do (
start "" "%%a"
)

Related

Delete files with specific extension except a particular file

I have a folder which contains the following files:
How can I delete all files with extension .rst (File1.rst, File2.rst, File3.rst, File4.rst, File5.rst) except the file "index.rst" from a batch file.
I have tried this, but it's not working:
for /f "skip=1 delims=" %%i in ('D:\hfTools\Projects\Validation-Source\Docs\source /b "*.rst"') do #(if "%i" neq "index.rst" echo %i)
Any help would be welcome. Thank you.
Here's an example of how to perform the task using the ForFiles command, forfiles.exe:
#%__AppDir__%forfiles.exe /P "D:\hfTools\Projects\Validation-Source\Docs\source" /M "*.rst" /C "%__AppDir__%cmd.exe /D /C If #IsDir==FALSE If /I Not #FName==0x22index0x22 Del /A /F #File"
Here's an example of how to perform the task using the Dir command:
#Echo Off
SetLocal EnableExtensions
If Exist "D:\hfTools\Projects\Validation-Source\Docs\source\*.rst" (
PushD "D:\hfTools\Projects\Validation-Source\Docs\source" && (
For /F "EOL=? Delims=" %%G In ('Dir /B /A:-D "*.rst" ^
^| %__AppDir__%findstr.exe /E /I /L ".rst" ^
^| %__AppDir__%findstr.exe /I /L /V /X "index.rst"'
) Do Del /A /F "%%G"
PopD
)
)
My preference however would be to use the Where command, where.exe:
Example batch-file
#Echo Off
SetLocal EnableExtensions
For /F "EOL=? Delims=" %%G In ('%__AppDir__%where.exe ^
"D:\hfTools\Projects\Validation-Source\Docs\source":"*.rst" ^
^| %__AppDir__%findstr.exe /E /I /L /V "\index.rst"'
) Do Del /A /F "%%G"
You could even do that as a single line batch-file:
#For /F "EOL=?Delims=" %%G In ('%__AppDir__%where.exe "D:\hfTools\Projects\Validation-Source\Docs\source":"*.rst"^|%__AppDir__%findstr.exe /EILV "\index.rst"')Do #Del /A/F "%%G"
Or directly from a cmd window:
For /F "EOL=?Delims=" %G In ('%__AppDir__%where.exe "D:\hfTools\Projects\Validation-Source\Docs\source":"*.rst"^|%__AppDir__%findstr.exe /EILV "\index.rst"')Do #Del /A/F "%G"
And for an off topic bonus, because for general use it seems easier; use powershell instead:
Remove-Item -Path "D:\hfTools\Projects\Validation-Source\Docs\source\*.rst" -Exclude "index.rst" -Force
You could even run that using a one line batch file, if you really needed to:
#%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoP "RI 'D:\hfTools\Projects\Validation-Source\Docs\source\*.rst' -E 'index.rst' -Fo"
Not tested:
forfiles /M *.rst /C "cmd /c if #file!=index.rst del #file"
Explanation:
/M *.rst # only consider *.rst
if ... # #file is the filename as found by forfiles
# != is this the correct way say "does not equal"?
You are actually quite close, you just forgot to double a few %-signs and you missed the dir command:
rem // Change to target directory, because `dir /B` only returns pure file names:
pushd "D:\hfTools\Projects\Validation-Source\Docs\source" && (
rem // Capture the output of `dir /B` and loop through the lines/files:
for /f "delims= eol=|" %%i in ('
dir /B /A:-D-H-S "*.rst"
') do #(
rem // Check file name against predefined exception:
if /I not "%%i"=="index.rst" (
rem // Actually delete the currently iterated file:
ECHO del "%%i"
)
)
popd
)
Once you are satisfied with the output, remove the upper-case ECHO command to actually delete files.
Note that the pattern *.rst in the dir /B command line is actually checked against both long and short file names (if the latter is enabled), so it actually matches files whose extensions begin with rst (if applicable).
If the files to delete always match the pattern File*.rst you do not even need the if condition:
rem // Change to target directory, because `dir /B` only returns pure file names:
pushd "D:\hfTools\Projects\Validation-Source\Docs\source" && (
rem // Capture the output of `dir /B` and loop through the lines/files:
for /f "delims= eol=|" %%i in ('
dir /B /A:-D-H-S "File*.rst"
') do #(
rem // Actually delete the currently iterated file:
ECHO del "%%i"
)
popd
)
This is easily done using PowerShell. If you are on a supported version of Windows, PowerShell will be available. When you are confident that the correct files will be removed, remove the -WhatIf from the command.
Remove-Item -Path './*' -Include '*.rst' -Exclude 'index.rst' -WhatIf
Id you must do it from a cmd.exe (.bat file) script:
powershell -NoLogo -NoProfile -Command ^
"Remove-Item -Path './*' -Include '*.rst' -Exclude 'index.rst' -WhatIf"

How to ignore 8.3 filename for "move" command in batchscript?

I have to move a lot of xml-Files, named as follow:
F010199004524001_904.XML
F010199805946001_737.XML
F010199904725001_611.XML
F030390114543001_901.XML
F030390114544001_257.XML
F030390114545001_901.XML
in my batch-file, there's this line:
move C:\source\F01*.xml C:\target\F01\
Now I have the problem, that some of the files have a different 8.3-filename, for example
"F030390114545001_901.XML" has the name "F01FCF~1.XML" so this file is also moved in the directory "F01".
The command "fsutil 8dot3name strip" in my case is useless, because the batch is running on a 2003 server.
Is there any type of workaround or alternative commands to solve this problem?
for /f %%a in ('dir /b /a:-d "c:\source"^| findstr /i /b "F01"^|findstr /i /e ".xml"') do (
move "%%~fa" "C:\target\F01\"
)
this?
EDIT
findstr filtering can be done with a single expression -
findstr /beri "f01.*xml"
so
for /f %%a in ('dir /b /a:-d "c:\source"^| findstr /beri "f01.*xml"') do (
move "%%~fa" "C:\target\F01\"
)

Batch file / add file names to command

Looking for a way to run a command, but insert the path/filenames of all mp4 files from the working directory (where the script is run) to the command.
vlc.exe c:\path\filename1.mp4 c:\path\filename2.mp4
I have the following code, but the "%%~A" is only inserting one path/filename at a time instead of adding every path filename from the folder.
set dir=C:\Users\Administrator\Desktop\1
for /f "delims=" %%A in ('dir /b "%dir%\*.*"') do ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%%~A" --sout=#transcode{vcodec=mp2v,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/abc} --sout-keep --loop)
An elementary solution: first try
set "dir=C:\Users\Administrator\Desktop\1"
dir /b /a:-d "%dir%\*.*"
The same result (filenames only) as in:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%%~A"
So add full path as follows:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%dir%\%%~A"
Eventually, replace #echo "%dir%\%%~A" with your command...

How can I Delete Files That Do Not Contain Certain Characters Using Windows Batch File?

How can I delete files that do not end with the pattern .PDF_*.pdf ?
AA00A6E2.PDF
AA00A6E3.PDF
AA00A6E3.PDF_01.pdf
AA00A6E3.PDF_02.pdf
AA00A6E3.PDF_03.pdf
I have been trying all sorts of variations around the following syntax:
FOR %%F IN (%varFolderSource%\*.*) DO IF NOT "%%~xF" == "*_*" DEL /F /S "%%F"
But I can't seem to crack it.
%varFolderSource% is a folder path: C:\Temp etc.
I am running this in a Windows 7 batch file.
For /f "delims=" %A in ('dir /b /a-d ^|findstr /i /v /e /r "\.PDF_[a-z0-9]*\.pdf"') do echo %A
Type
for /?
findstr /?
In a batch file use %%A rather than %A at command prompt.

How to create file in partially known folder name using batch file

I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)

Resources