Space in batch file path - windows

I'm trying to run a simple batch file.
for /f %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
this only works if there is no space in the path of the batch file.
the batch is in:
C:\Users\Rafael\Documents\Visual Studio 2012\Projects\vorace\Release\vorace.exe
the files I want to loop through are in:
C:\Users\Rafael\Documents\Visual Studio 2012\Projects\vorace\Release\EnsembleIndependant
can anyone help me.
thanks.
thank you guys for the suggestions.
for /f "delims=" solved my problem.

for /f "delims=" %%a IN ('dir /b /s "%~dp0EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
%~dp0 has already a backspace at the end.

for /f "delims=" %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
space is a standard delimiter in batch for loops as well as <tab> and with "delims=" you deactivate it.

FOR /F uses as standard delimiters space and tab, to avoid this you need to add the options.
for /f "delims=" %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)

Related

Batch rename in Windows based on specific match

Say I have the following files:
chromadenoisecfg5 - Copy.cpp
chromadenoisecfg5 - Copy.h
chromadenoisetest5 - Copy.cpp
chromadenoisetest5 - Copy.h
imagecfg5 - Copy.h
What I'd like to do is rename them to:
chromadenoisecfg6.cpp
chromadenoisecfg6.h
chromadenoisetest6.cpp
chromadenoisetest6.h
imagecfg6.h
What I've got so far is dir /b | findstr /i "*\b5 - Copy\b*", which lists all the files I'm interested in, but I'm not sure how to further process them in terms of renaming.
Furthermore, the solution revolves around the string 5 - Copy. How can this be achieved in a generic way, with the string passed as an argument?
Here is another batch one-liner:
for /F "delims=" %%A in ('dir /B /A:-D "*5 - Copy.*"') do #(set "NAME=%%A" & setlocal EnableDelayedExpansion & ren "!NAME!" "!NAME:5 - Copy=6!" & endlocal)
This simply replaces the partial string 5 - Copy by 6 using the sub-string replacement syntax. For this to work, delayed expansion is necessary.
If you want to enter it in command prompt (cmd) directly, use this:
for /F "delims=" %A in ('dir /B /A:-D "*5 - Copy.*"') do #(set "NAME=%A" & cmd /V /C ren "!NAME!" "!NAME:5 - Copy=6!")
Batch oneliner - UNTESTED:
for /f "tokens=*" %A in ('dir /B') do for /f "tokens=1-3 delims=-. " %a in ("%A") do ren %A %a.%c

Batch script to move files that contain certain string

Hi guys just got the following code script running to search in a directory for files that contain the string "ABC" and move them to the directory at the end.
for /f "eol=: delims=" %%F in ('dir /b^|find "ABC"') do move /Y "%%F" "C:\DESTINATION_DIRECTORY"
Was wondering how to modify this to not have to be run from the input directory, i.e to add a SOURCE_DIRECTORY variable so I can run this script from elsewhere but have it parse thru the SOURCE_DIRECTORY.
Thanks for any help.
#echo off
set "root=c:\st"
pushd %root% && (
for %%# in ("*ABC*") do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"
)
popd
or
for /f "tokens=* delims=" %%# in ('dir /b "%root%\*ABC*"') do echo move /Y "%root%\%%~nx#" "C:\DESTINATION_DIRECTORY"
for recursive search:
for /f "tokens=* delims=" %%# in ('dir /b /s "%root%\*ABC*"') do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"

I'd like to delete a bunch of files in a folder that have a specific extension and do not match a specified string

I am new to command prompt scripting and batch files. I have a folder with the following:
file1.pdf
file1.tif
file1_cropped.tif
file1.txt
file2.pdf
file2.tif
file2_cropped.tif
file2.txt...
filen.pdf
filen.tif
filen_cropped.tif
filen.txt
I would like to delete all the tif files that do not have "_cropped" in the filename. I have seen a few solutions for deleting files that have a specified extension, or that match a specific string, but I am trying to combine the two.
Much thanks,
Marc.
for /f "delims=" %%a in ('dir /b /a-d *.tif^|find /v /i "_cropped"') do echo del "%%a"
should suit.
perhaps you'd want
pushd "target directoryname"
for /f "delim...
popd
to specify a directory other than your current to be processed.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
From command line, being in target directory:
for /F "eol=: delims=" %a in ('dir /b *.tif ^| find /V "_cropped"') do #del "%a"
Where we have:
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
In batch file:
for /F "eol=: delims=" %%a in ('dir /b *.tif ^| find /V "_cropped"') do #del "%%a"
A sample where we use a more interactive approach with confirmation:
Use typically as:
the_bat_file.bat exclude_these tif
Where option one is string in file names to exclude and two is file extension.
#echo off
set pat=_cropped
set ext=tif
IF "%1"=="--help" (
echo Usage %0 [exclude] [extension]
echo Default exclude : %pat%
echo Default extension: %ext%
goto end
)
GOTO part1
:confirm
SET /P CONF=Continue y/N?
IF /I "%CONF%" NEQ "Y" GOTO end
goto %con%
:part1
setlocal
IF NOT "%1"=="" set pat=%1
IF NOT "%2"=="" set ext=%2
echo Pattern : %pat%
echo Extension: %ext%
echo.
set con=part2
goto confirm
:part2
echo.
echo Files to delete:
echo.
for /F "eol=: delims=" %%a in ('dir /b "*.%ext%" ^| find /V "%pat%"') do echo "%%a"
echo.
set con=part3
goto confirm
:part3
for /F "eol=: delims=" %%a in ('dir /b "*.%ext%" ^| find /V "%pat%"') do DEL "%%a"
:end
endlocal

Setting a start position in a Path(Batch processing)

i'm a newbie in batch scripting,started learning this from last week only and this is the first question that i'm asking here.here is my situation,
Consider this example,this lists all directories under D:/Jose/test1 and append this to a text file.
Code:
#echo off
SETLOCAL EnableDelayedExpansion
cd /d D:\Jose\test1
FOR /F "delims=" %%G in ('dir /ad /on /s /b') DO (
ECHO %%~pG%%~nG>>D:\test2\list.txt
)
ENDLOCAL
pause
Text file output :
\Jose\test1\1
\Jose\test1\2
\Jose\test1\1\12
\Jose\test1\1\13
\Jose\test1\1\12\131
\Jose\test1\1\12\Copy of 131
\Jose\test1\1\12\131\1311
\Jose\test1\1\12\131\1311\13111
\Jose\test1\1\12\131\1311\13112
\Jose\test1\1\12\Copy of 131\1311
\Jose\test1\1\12\Copy of 131\1311\13111
\Jose\test1\1\12\Copy of 131\1311\13112
\Jose\test1\1\13\132
\Jose\test1\1\13\132\1321
\Jose\test1\1\13\132\1321\13211
I want to remove '\jose' from all line ie, i want to set '\test1' as the starting path. Need help guys..Thanks in advance...
try it with sed for Windows:
for /d /r %%G in (*) do sed -r "s/^\\[^\]+(\\.*)/\1/" "%%~pnxG">>D:\test2\list.txt
..solution with pure batch:
#echo off &setlocal
(FOR /f "tokens=2* delims=\" %%a IN ('dir /ad /on /s /b') DO ECHO(\%%~b)>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE
..and more batch:
#echo off &setlocal
(FOR /d /r %%G in (*) DO (
SET "fname=%%~G"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fname=!fname:*\Jose=!"
ECHO(!fname!
ENDLOCAL
))>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE

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

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"
)

Resources