How to run *.exe /key from the .bat in loop - windows

I have directory structure:
DIR
|-UNINSTALL.BAT
|-component_name
|-source
|-setup.exe
|-uninst.bat
|-another_component_name
|-source
|-setup.exe
|-uninst.bat
|-yet_another_component_name
|-source
|-setup.exe
|-uninst.bat
and so on...
In every directory like "component_name" I have setup.exe file which installs current component to the palette component in Delphi.
uninst.bat contains only this string:
"setup.exe" /uninstall
So I need to write UNINSTALL_ALL.bat in DIR that would run the uninst.bat in all component directories.
Thank you in advance.

you could do it with this line:
for /f %%a in ('dir /b /s uninst.bat') do call %%a
note that the '%%' is necessary for batch files. if you are testing this on the command line, only use one '%'

That is kind of akward in a batch file. Though you could probably do it with the foreach statement. I would suggest though that you have a look at Powershell which will definitely give you the power to do this simply and a whole lot more if you want it.

You want to use the "for" construct. Something like this:
for %%i in (component_name another_component_name yet_another_component_name) do %%i\uninst.bat
The double-escaping (%%) is necessary if you put the "for" loop in a batch file. If you are just typing it out in a command prompt, only use 1 %.
Also, you may be able to use a wildcard to match against the directory names, if they follow some convention. Open up a command prompt and run "for /?" to see everything it can do...I believe there is a /d option to match against directories. That would look something like:
for /D %%d in (component_*) do %%d\uninst.bat
(obviously, adjust the wildcard to match your component directories.)

This should work:
FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B %%a
if you want them to wait each other, use this:
FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B /WAIT %%a

The way you describe your problem, you have only one level of sub-directories and you always call the same batch, from the root. Therefore:
Uninstall_all.cmd
#echo off
for /F "delims=" %%d in ('dir /b /ad') do cd "%%d"& start /b /w ..\uninstall.bat& cd ..
Should do the trick.

Related

Windows Cmd line delete command to delete all file except one

I am looking for a single windows command where I need to delete all the files in a folder except one.
eg: Files listed under directory:
c:\users\admin\folder
abc
abcde.zip
abcdef
123.txt
Now, I want to delete all the files except "abcde.zip"
corresponding Linux command would be: rm -rf !(abcde.zip)
Is there anything like this on windows without using batch script, just a single line command?
Thanks in Advance!
Since Win's command interpreter doesn't have a rm -rf like command (that works on both files and dirs) we just need to modify the command that I placed in my 1st comment, actually split it in 2 commands (still in a single line: well, if the console is really really wide :) ), and execute them in a sequence (&):
one that takes care of the files (the /a:-d argument for dir), and launches del
one that takes care of the folders(the /a:d argument) and launches rmdir
Here's what it looks like (note that it does the job for the current directory):
(for /f "tokens=*" %f in ('dir /a:-d /b 2^>nul ^| findstr /v /b /e /c:"abcde.zip"') do (del /f "%f")) & (for /f "tokens=*" %f in ('dir /a:d /b ^| findstr /v /b /e /c:"abcde.zip"') do (rmdir /q /s "%f"))

How to recursively change filenames for upper to lower case through bat file

I have been expanding on the issue solved here: https://superuser.com/questions/65302/is-there-a-way-to-batch-rename-files-to-lowercase/412413#412413
I wish to change filenames from upper case to lower case and since files are stored in mulitple folders I wish to do it recursively.
I have tried the following:
setlocal EnableDelayedExpansion
CD /D "somefolder"
FOR /D %%G in (*) DO (
FOR /F "Tokens=*" %%f in ('DIR %%G /l/b/a-d') DO (RENAME "%%f" "%%f"))
I get "the system cannot find the path specified errors".
I am sure I am overlooking something obvious.
Your code gives the path error because RENAME cannot find the file. The DIR command lists files in a subdirectory, but your current directory is not the subdirectory.
You have other problems - you are not doing a recursive folder search. The /D option only lists immediate child folders. Your code would miss files in the root "somefolder", as well as any folders that are two or more levels deep.
Also, the original code from SuperUser is flawed. The use of "tokens=*" will strip leading spaces. It is possible (however unlikely) for a file name to begin with a space, and then the code would break. One correct syntax to use is for /f "eol=: delims=" ....
The MichaelS answer using the dir /s option cannot work because the REN command does not accept path information in the target - only the file name and extension can be used. Normally you would solve that problem by using %%~nxF, but that reverts to the original case of the file name!
Here is a proper recursive solution for use on the command line:
for /r "somePath" %D in (.) do #for /f "eol=: delims=" %F in ('dir /l/b/a-d "%D"') do #ren "%D\%F" "%F"
And from a batch script
#echo off
for /r "somePath" %%D in (.) do for /f "eol=: delims=" %%F in ('dir /l/b/a-d "%%D"') do ren "%%D\%%F" "%%F"
If you are willing to go beyond native cmd.exe commands, then another option is my JREN.BAT regular expression renaming utility that supports options to convert names to upper or lower case. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward - no 3rd party exe files needed. Full documentation is built in - accessed from the command line via jren /?, or jren /?? if you want paged output.
With JREN, the recursive solution is as simple as:
jren "^" "" /s /l
You don't need to loop over the subdirectories. Simply add /s to the dir command:
FOR /F "Tokens=*" %%f in ('DIR /l/b/a-d/s') DO (RENAME "%%f" "%%f")
/s will include all subfolders recursively.

Locate tool.exe through search function and then execute it

I'm making batch-files for fun and i ran in to a problem. I've search for a solution and i havn't found anyone.
I'de like to code my batch file so it will search for a specific file on my c: drive and after it found the pathlocation I want it to execute. Can it be done?
I don't know if i can combine the path I get from "dir tool.exe /s /p" somehow.
Using batch
for /f %%i in ('dir C:\tool.exe /s /b') do (
start %%~fi
exit /b
)
Consider the fact that if your executable is located deep down the folder tree it will take some time to actually find it and run. So if you want to improve the performance you might narrow down a search a bit if it is an option of course.
Try this:
for /f "delims=" %%A in ('dir tool.exe /s /p') do set "var=%%A"
start %var%
The output of 'dir tool.exe /s /p' is saved in var which you can then use to do whatever you need to do.
I know you asked for batch, but here is as a powershell option because I was bored:
Get-ChildItem / -Recurse -File | % { if($_.Name -eq "notepad.exe") { &$_.FullName; break} }
It will execute the first hit on the search for "notepad.exe" and then exit the script.

Windows batch - match folders with a dot (.) in the folder name

I'm trying to figure out how to match folders with a dot in the file name (e.g., ".svn") in a Windows batch script.
Here's the basic script:
setlocal
pushd c:\myDir
#echo off
FOR /D /r %%G in ("*\.whatever") DO (
echo %%G
REM do stuff
)
#echo on
popd
endlocal
This works just fine for most folder names (e.g., "*bin"), but I can't figure out the method to specify a folder with the dot. "*.whatever" and "*\.whatever" return no results. I'm guessing I'm missing some escape character or something equally simple, but I haven't been able to find any documentation on it.
(Before anyone asks, no I'm not trying to recursively delete subversion folders; "*.svn" is just an example.)
Maybe I am missing something, but as you say it seems simple
for /r /d %%a in (.*) do echo %%~fa
But if the folders are hidden, the normal for will not be able to see them, so we need to execute a dir command an process its output with a for /f
for /f "delims=" %%a in ('dir /ad /s /b .*') do echo %%~fa

Move files in subfolders to a folder and rename windows prompt

I have almost the final command line, but I need the rename part of it, to reach the goal I want I have this:
for /f “tokens=*” %a in (‘dir /b /s /a-d’) do #copy “%a” “C:\YourFolder” /y
and it works fine, but in my case I have tons of folders with only one file on each one, this file has the same name file.ext, so, is there any way to move and change the name, for example like file1.ext, file2.ext,...
Thanks!
This is not foolproof but it's likely to work, and will prompt if the small likelihood occurs that a filename exists. You'll need a batch file to make it foolproof and to improve the naming strategy.
Remove the #echo if you are happy with the result.
cmd /v:on /c for /f "delims=" %a in ('dir /b /s /a-d') do #echo copy “%a” “C:\YourFolder\%~na-!random!!random!!random!%~xa” /-y

Resources