Batch Rename Subdirectories in Windows - windows

I have a directory with thousands of subdirectories that contain their own subdirectories that need to be renamed. I'm using a Windows 7 machine that I do not have Administrator rights for so I can't download a simple program to do this for me.
Right now I have a test directory C:\test with a few subdirectories that have subdirectories named old that I am trying to change to new using a batch file.
Just to be clear I want the following:
C:\test\1\old
C:\test\2\old
C:\test\3\old
to become
C:\test\1\new
C:\test\2\new
C:\test\3\new
Thank you for any help you can provide.

Here's what I came up with quickly. I ran a quick test locally and it seemed to do what you're asking for:
#echo off
FOR /D %%D IN ("C:\test\*") DO CALL :RENAME %%D
:RENAME
SET CRITERIA=\old
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "new"
Save that to a bat file and give it a shot. Hopefully that helps.

Justin's answer was really helpful for my similar problem, though by default it only handled a simpler pattern for \A\B\C where A is a base directory, B is some undetermined directory and C is the directory you seek.
I modifed his script to recurse the base directory A through any number of layers until it finds C.
Here's the script, written to expect command line paramers:
#echo off
set BASEDIR=%1
SET CRITERIA=\%2
SET REPLACENAME=%3
call :FindDirs %BASEDIR%
GOTO END
:FindDirs
FOR /D %%F IN ("%1\*") DO CALL :RENAME %%F
GOTO:EOF
:RENAME
REM echo DIR=%1
FOR /D %%R IN ("%1%CRITERIA%") DO (
if EXIST %%R RENAME %%R "%REPLACENAME%"
)
call :FindDirs %1
GOTO:EOF

Related

Batch Script to Find a Folder inside Sub Folders and get Path

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"

Batch script to automatically delete empty folders - need help adding exceptions

My batch experience is rather limited but I did manage to write the following script to delete all empty subfolders of my target folder.
set "Target=C:\Target"
for /f "delims=" %%i in ('dir "%Target%" /A:D /B /S ^| sort /r') do rd "%%i" 2>NUL >NUL
My problems are:
1. I would like to be able to keep the first layer of subfolders intact and only delete empty folders in the following layers.
2. If it is possible I would also like to skip certain folders completely based on their name.
Is it possible to do that or would i need to write the script for all of the subfolders i want to clean up?
The code below was tested on following folder structure in folder C:\Target
Folder1
Folder To Delete
Folder To Keep A
FolderToKeepB
Folder 2
Folder To Keep A
Not Empty Folder
File in Folder.txt
The resulting folder structure after running the batch file below was
Folder1
Folder To Keep A
FolderToKeepB
Folder 2
Folder To Keep A
Not Empty Folder
File in Folder.txt
Just the single folder Folder To Delete was deleted as this one should not be always kept and it was indeed empty.
#echo off
rem For each subfolder in C:\Target do ...
for /D %%D in ("C:\Target\*") do (
rem For each subfolder in found subfolder of C:\Target do ...
for /D %%S in ("%%~D\*") do call :DeleteFolder "%%~S"
)
rem This goto :EOF results in exiting the batch file.
goto :EOF
rem Subroutine for easier comparing the name of the current
rem subfolder with the name of the subfolders to always keep.
rem The goto :EOF commands below just exit the subroutine.
:DeleteFolder
if "%~nx1"=="Folder To Keep A" goto :EOF
if "%~nx1"=="FolderToKeepB" goto :EOF
rem Delete this subfolder which fails if it is not empty.
rem The error message is suppressed by redirecting output stream
rem stderr (standard error) with handle 2 to the NUL device.
rd "%~1" 2>nul
goto :EOF
For more details on the used commands read help output printed into a console window on running
For command call: call /? or help call
For command for: for /? or help for
For command goto: goto /? or help goto
For command rmdir (rd): rd /? or help rd or rmdir /? or help rmdir
Using command redirection operators

Batch script for renaming files

I need help regarding how to rename files automatically from a subfolders in the C:\temp_download which contains res_save.0 in those folders. I want to rename it as nvm_res.0 and then subsequently nvm_res.1... nvm_res.2. When copying them to the folder C:\temp, it keep replacing. Can you help me with the wildcards? the code is below.
for /R "C:\temp_download" %%f in (res_save.*) do xcopy "%%f" "C:\temp\num_res.*" /Y
When you get an answer that solves your issue, make sure you accept the answer so that future searchers get an idea of which solution to try first.
This might do what you need.
#echo off
set c=0
setlocal enabledelayedexpansion
for /R "C:\temp_download" %%f in (res_save.*) do (
set /a c+=1
copy "%%f" "C:\temp\nvm_res.!c!" /Y >nul
)

Batch File - FOR LOOP

Let me set the stage of my issue. I have a folder (FOLDER_ABC). Within that folder, there is a folder for my application, including a unique and always changing version number (application-v1.3.4). Within that folder, there is the application (application-v1.3.4.exe) - which will also change periodically.
C:\FOLDER_ABC\application-v1.3.4\application-v1.3.4.exe
In this section below I create a directory listing of the FOLDER_ABC for any folders starting with application* and store that folder name into a file called directory.txt. I then create a perameter and store that directory into it. I'm doing it this way, versus applying the direct full path to the directory or file, since the versions will change and I don't want to hard code the batch script.
cd C:\FOLDER_ABC\
dir application* /b /ad>"C:\FOLDER_ABC\directory.txt"
set /p verdir= <C:\FOLDER_ABC\directory.txt
Here is my issue. In the section below, I'm trying to get my batch script to run the application*.exe file, and continue on with my batch file. It currently runs my application, but it hangs and doesn't continue the rest of my batch script. I'm really new to all this coding so I appreciate the help. I assume it could be something related to me not closing the FOR loop properly? How can I get it to continue on to :FINISH?
cd "C:\FOLDER_ABC\%verdir%\"
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO %%G;
:FINISH
ECHO THE END
exit
Figured it out, but didn't have enough StackOverflow credits to answer my own question. My solution is listed below. Thanks everyone. You pointed me in the right direction.
cd "C:\FOLDER_ABC\%verdir%\"
FOR %%G in (*.exe) DO START %%G
you can try:
FOR /f "delims=" %%G IN ('dir /b /a-d *.exe') DO start "" "%%~G"
I'm not sure if this is your problem, but it is possible that the ; is causing problems. You do not terminate commands with ; in batch files.
There is no need for a temporary file. Your code can be greatly simplified with FOR:
pushd c:\folder_abc
for /d %%F in (application*) do cd "%%F"
for %%F in (*.exe) do "%%F"
:FINISH
ECHO THE END
exit /b
Try using the START command:
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO START %%G;
There may be other better ways if achieving what you want, for example, if you know that the exe always has the same name as its directory, and that there will be only one such directory, you could do the following:
FOR /D %%i in (application-v*) DO START %i\%i.exe
UPDATE
From comments:
My only issue, which I just realized, is that the application folder and application name are not always identical.
In that case, you could try something like:
for /d %%i in (application-v*) do for %%j in (%%i\*.exe) do start %%j

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