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
Related
I have 2 files in my folder. One is "TEST ONE.txt", the other is "LEARN NEW.vcd".
I need to create a batch script to rename the *.txt using the *.vcd.
So the Batch script should rename the "TEST ONE.txt file" to "LEARN NEW.txt".
I have only one file with txt and one file with vcd extension in my folder C:\DEV folder.
I tried using the following script but it does not pick up the full name of the vcd extension and drops everything after the spaces - so I just get LEARN.txt :(
#echo ON
CD "C:\DEV folder"
setlocal enabledelayedexpansion
for /F %%A in ('dir /b *.vcd') do (
set xname= %%~nA
ren *.txt !xname!.txt
)
Thanks - db
It appears that the default in FOR /F may be to only take one (1) token. You can force it to take all using tokens=*. Quoting is important.
#echo ON
CD "sub"
setlocal enabledelayedexpansion
for /F "usebackq tokens=*" %%A in (`dir /b *.vcd`) do (
set xname=%%~nA
ren "*.txt" "!xname!.txt"
)
Just in case someone stumbles across this the way I did, there is a more elegant solution that I was forced to devise because I wanted it to go through multiple sub-directories, rather than run it one directory at a time. It is:
forfiles /S /M *.vcd /C "cmd /c rename *.txt #fname.txt"
Use your file extensions of choice.
I'm trying to get path of a directory containing .snapshot folder. .snapshot should be searched in all parent and sub-directories.
I've a directory structure resembling following tree command output (only more complex, deployment deals a huge NAS drive)
My script, as below, so far lists only one out of potentially 100s of directories containing .snapshot
set Dir=C:\Vol
cd %Dir%
for /d /r "%Dir%" %%a in (*) do if /i "%%~nxa"==".snapshot" set "folderpath=%%a"
echo "%folderpath%"
Output:
C:\Vol\xnd76540\u44753\mike.smith\.snapshot
My Question
How can I check all sub-directories of a folder for .snapshot, come back to the parent and follow another path for again searching .snapshot in other set of sub directories and so on?
Performance tips appreciated.
Couldn't find a more relevant code snippet.
You have it almost done
for /d /r "%Dir%" %%a in (*) do if /i "%%~nxa"==".snapshot" (
echo %%~dpa
)
Or,
for /d /r "%Dir%" %%a in (.snapshot) do if exist "%%~fa" (
echo %%~dpa
)
The problem with the original for in the question is that it is assigning a variable while iterating, and when the for ends, the value echoed is the last assigned as in each iteration the value is overwritten.
Instead, echoing the value inside the running for you will have the full list.
use
set Dir=%1
cd %Dir%
for /d /r "%Dir%" %%a in (*) do if /i "%%~nxa"==".snapshot" set "folderpath=%%a"
echo "%folderpath%"
Save this Batch file say temp.bat
Now in another batch file call it by passing arguments like
temp.bat "C:\Vol1"
temp.bat "another location"
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
How can I copy the batch file to subdirectories, run it, then delete the batch file?
I am working on a batch file that will automatically change the folder icon of the folder it is in. Shown below:
#ECHO OFF
attrib +s "%CD%"
set ICODIR=%CD%\Icon\
for %%F in ("%ICODIR%"*.ico) do set ICO=%%~nxF
set ICOINI=Desktop.ini
IF EXIST Desktop.ini (
attrib -s -h %ICOINI%
)
echo [.ShellClassInfo] > %ICOINI%
echo IconResource=%ICODIR:~2%%ICO%>>%ICOINI%
echo InfoTip=%ICO:~0,-4%>>%ICOINI%
attrib -a +s +h %ICOINI%
Pause
This works, after many problems found out that it will not create the folder icon until a file is deleted within that directory.
I have been trying to work on a for loop that will list all sub directories and store their names. Though it lists the root directory first. How can I get it to skip the root directory? Code shown Below:
#ECHO OFF
for /R /D "delims=\" %%d IN (%CD%) do echo %%~nd
Pause
EDIT: Why is the file only made in the last folder?
for /D /R "%cd%" %%d IN (*) do (
set something=%%~nd
echo TEST>%something%\Desktop.txt
)
try this:
for /D /R "%cd%" %%d IN (*) do echo %%~d
%cd% wouldn't be listed.
Hmm.. Interesting scenario.
Try looking into the forfiles command at http://ss64.com/nt/forfiles.html .
However using the for command this is the best solution I could come up with.
:loop
set /a count+=1
for /f "tokens=%count% delims=\" %%a in ("%CD%") do (echo %%a >
output.txt)
if %count% == 8 exit
goto :loop
Note this is not the best answer but it works. This instance will check a maximum of 7 directories in from the drive letter but by incrementing the if check.
This Worked for me, hope it helps.
Yours Mona.
For the second part, I found that adding an IF statement allows you to filter out the root of the search. Your example didn't work at all for me, so I had to modify it a little. /R prepends the current dir in the search (not %CD%) to the loop variable, so I had to strip some sentinel value out. I deemed it unlikely enough to find files with "ZZZZZ" in the name, but your filesystem may need a different sentinel.
FOR /R "%CD%" %%d IN (ZZZZZ) do #(SET "D=%%~d" & SET "D=!D:\ZZZZZ=!" & IF NOT "!D!"=="%CD%" #ECHO !D!)
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*