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.
Related
I want to remove the part of a filename after the third "_" from thousand of files. The structure after the third "_" varies and contains "_" in some cases. The length of the first part varies so I can't just remove the first 15 characters. The result should be unique.
The filenames look like this:
00_TEXT_=Text00._AA1234L_AA1_1.pdf
00_TEX_=Text00._AA1234L_AA1_2.pdf
00_TEXT_=TextText00._DD2023A.pdf
00_TEXT_=Text00._AA2345L_BB1_1.pdf
00_TEXT_=Text00._AA2345L_BB1_2.pdf
The result should look like this:
AA1234L_AA1_1.pdf
AA1234L_AA1_2.pdf
DD2023A.pdf
AA2345L_BB1_1.pdf
AA2345L_BB1_2.pdf
Any idea why this is not working:
#echo off
setlocal enabledelayedexpansion
set deletestring=*_*_*_
for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)
I was able to get it working with this:
#echo off
setlocal enabledelayedexpansion
set deletestring=*_*_*_*
for /f "tokens=1,2,3,* delims=_" %%F in ('dir /b "%deletestring%"') do (
Ren "%%F_%%G_%%H_%%I" "%%I"
)
endlocal
Note that enabledelayedexpansion isn't really needed in the above.
Alternately, you could do this as a single line (no batch file needed):
for /f "tokens=1,2,3,* delims=_" %F in ('dir /b "*_*_*_*"') do Ren "%F_%G_%H_%I" "%I"
The idea is to simply split the matching filenames apart by underscores and then reconstruct the names during the rename process (%%F_%%G_%%H_%%I gives the original file name when going through the loop). Then rename the file to everything after the 3rd underscore, which is the %%I value.
Your FINDSTR search is wrong - a string of any characters (wildcard) is .*, not *.
Variable find/replace does not support wildcards, except for the !var:*search=! syntax that replaces everthing up until the first occurrence of "search".
There is no need for FINDSTR, all you need is DIR with normal wildcard masking.
You can use FOR /F to parse the name into tokens. I use two loops - the first to get the entire name, and the second to parse out the portion after the 3rd _.
The following should work:
#echo off
for /f "eol=: delims=" %%A in (
'dir /b /a-d *_*_*_*'
) do for /f "tokens=3* delims=_" %%B in ("%%A") do ren "%%A" "%%C"
Or you could use my jren.bat utility that renames files using regular expression replacement. It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
jren "^(.*?_){3}" ""
Use CALL JREN if you put the command within another batch script.
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.
I have a directory with thousands of files, and I need to find specific filenames by file count (files are sorted by name or date).
Is there an easy way to make it using cmd commands?
Thank you.
If you want the 11th file then you want to skip the first 10. Here is a simple batch file that does the trick. Change the DIR command in the FOR IN() clause to get the results you need.
#echo off
for /f "skip=10 eol=: delims=" %%F in ('dir /b /a-d') do set "chosenFile=%%F" & goto break
:break
echo The 11th file is %chosenFile%
exit /b
If all you want to do is list the 11th file to the screen, then you can do this on the command line:
cmd /c "for /f "skip=10 eol=: delims=" %F in ('dir /b /a-d') do echo %F&exit"
I have a txt file that contains the following lines
jfo3 93jfl
lvls 29fdj
nskd jfuwe
xlkw eklwe
I'm trying to read the file line by line, and do something with it. What delimiter should I use?
The delim I'm using here reads each word separately.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (lines.txt) do (
echo %%x
)
This reads line by line for me:
for /f "delims=" %x in (lines.txt) do echo %x
The problem is not related to delims, but to tokens:
for /f "tokens=*" %%x in (lines.txt) do echo %%x
If this is your input file:
abc,def
ghi,jkl
mno,pqr
then use
FOR /F "tokens=1,2,3 delims=," %%i in (test.txt) do (whatever u want)
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"