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

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

Related

Is there any way for windows batch script or command prompt to list the directories from a certain folder only without including the hard drive?

I tried using dir /b/s *.png to list the directories of all my png files. the results were like this:
D:\Newfolder\test\images\approved\11.png
D:\Newfolder\test\images\approved\12.png
D:\Newfolder\test\images\approved\13.png
D:\Newfolder\test\images\approved\14.png
D:\Newfolder\test\images\approved\15.png
D:\Newfolder\test\images\approved\16.png
D:\Newfolder\test\images\approved\17.png
D:\Newfolder\test\images\approved\18.png
D:\Newfolder\test\images\approved\19.png
D:\Newfolder\test\images\approved\20.png
I want it to be shortened so it will display only starting from the images folder;
images\approved\11.png
images\approved\12.png
images\approved\13.png
images\approved\14.png
images\approved\15.png
images\approved\16.png
images\approved\17.png
images\approved\18.png
images\approved\19.png
images\approved\20.png
is it possible to do?
if it's not, is there any way for me to edit the directories by deleting the first few folder from a generated text file?
say i put dir /b/s *.png > path.txt how do i edit the texts since the list got no whitespaces.
i'm still new to this so i'm not so familiar with much commands but this is as far as my understanding can do.
Assuming this comment in your question:
"I want it to be shortened so it will display only starting from the "images" folder;"
you always want from the images folder, and also assuming the images folder always exists in the path:
#echo off & setlocal enabledelayedexpansion
for /R %%i in (*.png) do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
Or to explicitly ensure you only include paths with \images\ in the path:
#echo off & setlocal enabledelayedexpansion
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
example of using pushd:
#echo off & setlocal enabledelayedexpansion
pushd "%~1"
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
popd
So the above you can run from cmd and simply pass the path you want the script to run in as script_name.cmd "C:\some path\here"

Adding file extensions to files without them in Windows

I am trying to add file extensions to a large number of files located in a series of folders and subfolders in Windows. For some reason, these files do not have a file extension on them and I need them to have the extension .ddd so I can convert them to PDFs using a separate program.
cd R:\PRODUCTION\92
ren *. *.ddd
Note that this command does indeed work but only on folders that actually contain the files I need, and no subfolders. What could I add or change to hit all files in all subfolders? Thanks in advance.
Please try the following command. If it looks like it will do the correct rename, remove the ECHO from the REN command line.
CD /D R:\PRODUCTION\92
FOR /F "delims=" %f IN ('DIR /S /B /A:-D "*."') DO (ECHO REN "%~f" "%~nf.ddd")
In a .bat file script, double the percent character on the variable.
CD /D R:\PRODUCTION\92
FOR /F "delims=" %%f IN ('DIR /S /B /A:-D "*."') DO (ECHO REN "%%~f" "%%~nf.ddd")

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.

Windows batch file - display all sub-folders

I'm having difficulty returning JUST folders (ignore files) using a windows batch file.
Here's what I have right now. Currently it's returing files and sub-sub-folders.
for /r %%g in ("xx*") do echo %%g
Also, say I want to only return the folders that start with a couple different prefixes.
For example: I want to echo only the folders that start with w*, we*, cm*, cr*, etc under within in the folder "work". I do Is this possible using a batch file?
Thanks.
You can use the dir command with the modifier /a:d which will tell it to only search directories
FOR /f "tokens=*" %%i in ('DIR /a:d /b w*') DO (
ECHO %%i
)
This will find all of the subfolders that start with w*
Here's modified version of Andrew's answer that can handle multiple prefixes:
dir /a:d /b w* we* cm* cr*

How do I iterate over a set of folders in Windows Shell?

I'm currently trying to write a .cmd Windows Shell script that would iterate over a set of folders. However even the following simplest script:
echo "%ROOT%"
for %%f in ("%ROOT%\Binaries\" ) do (
echo "%%f"
if not exist "%%f\Subfolder"
md "%%f\Subfolder"
)
outputs:
CurrentDir>echo "<ActualPathToRoot>"
"<ActualPathToRoot>"
%f\Subfolder was unexpected at this time
CurrentDir>if exists "%f\Subfolder"
What am I doing wrong? How do I alter that script so that it iterates over that one folder and once it see there's no subfolder named "Subfolder" it creates that subfolder? Also is there a good tutorial on writing such scripts?
For (sub)folder-iteration you need to use a different for parameter.
So if you want to list all directories of C: you should do this:
for /d %%A in (C:\*) do echo %%A
Note the parameter /d which indicates a directory. To go into subdirectories you need to do a recursive for with /r
for /r C:\Windows %%A in (*.jpg) do echo %%A
This would iterate through all Windows subdirectories looking for JPGs. Low behold you should be able to do /d /r and this reference suggests you can - I simply can't, but maybe you are able to do this?
A workaround I quickly jotted down is to just do a dir of all directories in a for loop:
for /f "delims=" %%A in ('dir /ad/s/b') do echo %%A
Note that dir is used in conjunction with /ad/s/b which performs a recursive listing of directories, printing the names of the directories found.
With these tools in your hand you should be able to do your if-subfolder construct. Note that you might need
This works for me:
echo %ROOT%
for /D %%f in (%ROOT%\Binaries\*) do echo %%f && if not exist %%f\Subfolder md %%f\Subfolder

Resources