How do I modify this:
for /f %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
to work when the path contains spaces?
For example, if this is run from
c:\my folder with spaces
it will echo:
c:\my
Thanks
You need to use:
for /f "delims=" %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
This overrides the default delimiters which are TAB and SPACE
I got around this by prepending "type" and putting double quotes surrounding the path in the IN clause
FOR /F %%A IN ('type "c:\A Path With Spaces\A File.txt"') DO (
ECHO %%A
)
This article gave me the idea to use "type" in the IN clause.
If you don't want to deal with "quotes" you can use the "s" switch in %~dpnx[]...
this will output the short filenames that are easy to work with.
from the Command line...
for /f "delims=" %f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %~sdpnxf
inside a .CMD/.BAT file you need to "escape" the [%] e.g., double-up [%%]
for /f "delims=" %%f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %%~sdpnxf
The problem is there's a wildcard in the string that gets interpreted as a filename. You also need double quotes for a string with spaces. I'm not sure if there's a way to escape the wildcard.
for %a IN ("dir /b /s build\release\.dll") do echo %a
"dir /b /s build\release\.dll"
Related
The issue is that when I put this line inside cmd file it doesn't work and I have an error on command line window
for /F "tokens=*" %i in (dir /S /B *.log) do del %i
Error :
Unexpected variable i (translated from french because I have french windows)
Any idea please ?
for /F "tokens=*" %%i in ('dir /S /B *.log') do del "%%i"
^^ ^ ^ ^^^ ^
Percent signs in for replaceable parameters should be escaped inside batch files, replacing the single % with %%.
To execute a command with a for /f, you need to enclose the command with single quotes or, if the command itself contains single quotes, with backquotes and include the usebackq clause in the for /f options.
It is better to quote the file references to avoid problems with spaces or special characters
Anyway, this command can be written as
del /s *.log
instead of executing a del operation for each file.
From command line:
for /F "tokens=*" %i in ('dir /S /B *.log') do del "%~i"
or if used in a batch:
for /F "tokens=*" %%i in ('dir /S /B *.log') do del "%%~i"
Explanation:
surround dir /S /B *.log with single quotes;
enclose file name in double quotes as follows del "%~i" as it could contain spaces...
Resources (required reading):
An A-Z Index of the Windows CMD command line
Windows CMD Shell Command Line Syntax
Thank you all of you for your helps. Please excuse my late answer.
Here is the solution I've finally adopted thanks to #MC ND
del /s *.log
del /s *.log0*
del /s *.tlog
rmdir /S /Q wls-admin\.wlnotdelete
rmdir /S /Q wls-myapp\.wlnotdelete
Hello i want to create a batch file that copies the files from the current directory to another.
for /F "usebackq" %%b IN (`DIR /B /S ""`) DO #(
XCOPY %%b %1
)
So Far so good.
MY problem are the whitespaces in directories.
So when the name of a directory is /Dir whitespaces end/
it does not copy it. "File not found - Dir"
Start bat file with destination
CopyFiles.bat "I:\testFolder*.*"
How can i work around this feature?
Try this:
for /f "delims=" %%b in ('dir /b /s ') do xcopy "%%~b" "%1"
Important is 1) set "delims=" and 2) enclose for loop and other variables in double quotes.
In a Windows cmd batch script named my.bat, I want to execute a command for all the files in the current directory except for the batch file my.bat.
I use below command in my.bat currently to run the command only for *.txt *.ppt, but really as new files of different extensions might be added to the folder and hence execution of this command by excluding one type of file extension (in my case *.bat) would be more readable/helpful.
FOR /F "delims=|" %%i IN ('dir /b *.txt *.ppt') DO echo %%i
Question is: How do I exclude that file alone with that particular file extension and make the for command execute on all files of all extensions except the excluded one?
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do if not %%~xi==.bat echo %%i
I have added tokens=* in as well otherwise you won't get full filenames if they have spaces.
To echo without the dot
setlocal enabledelayedexpansion
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do (
set e=%%~xi
set e=!e:.=!
echo !e!
)
This is providing that the file doesn't have any other dots, otherwise it will remove them too. This is a bit more sturdy than just removing the 4th character from the end though, as not all files have a 3 character extension.
You could pass the output of the dir /b command through findstr, like so:
FOR /F "delims=|" %%i IN ('dir /b ^| findstr /v /r "^my.bat$"') DO echo %%i
The /v option to findstr prints anything that doesn't match the parameter. The match is based on a regular expression that matches only lines that contain my.bat and nothing else.
This is my for loop that i iterate over a folder
for /f %A in ('dir /b "G:\Files Sample\Samples\zip\txt"') do echo "%A"
My problem is the echo statement does not echo the complete name of the file when the filename contains spaces in it.
How do i correct this ?
This will do what you want:
for /f "delims=" %A in ('dir /b "G:\Files Sample\Samples\zip\txt"') do #echo %A
You just needed to set delims to nothing so that it didn't split the input into columns on spaces.
In a Windows command prompt, how can I rename all the files to lower case and remove all spaces?
Make a batch file
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ( ' dir /b /a-d *.* ') do (
set name="%%i"
set newname=!name: =!
rename "%%i" !newname!
)
NOTE: Run under a test directory and see if you have the expected result. I have not tested it.
EDIT: Forgot to say this will only remove the spaces.
I used this batch file to rename all folders and subfolders to lowercase names:
#ECHO OFF
CALL:GETDIRS
:GETDIRS
FOR /F "delims=" %%s IN ('DIR /B /L /AD') DO (
RENAME "%%s" "%%s"
CD "%%s"
CALL:GETDIRS
CD ..
)
GOTO:EOF
To make the trick of "lowercase" and "remove spaces" ...
In the given solution, in the 'dir' statement, use also "/l"
The /L statement in dir forces to lowercase the filenames in the
result.
As "Windows-RENAME" command, if you use the "same" filename, it will note convert from uppercase to lowercase.
ren XPTO.TXT xpto.txt
The result will always be : XPTO.TXT
To 'bypass' this, we use the ephemeral technique:
move old to temp, then -> move temp to new
Then the solution would be:
#echo off
if exist temporaryfilenametorename del temporaryfilenametorename /f/q
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir *.csv /l /b /a-d') do (
set name="%%i"
set newname=!name: =!
rename "%%i" temporaryfilenametorename
rename temporaryfilenametorename !newname!
)