Recursive directory processing in a BAT file with a twist - windows

OK, I apologize ahead of time for a) using an old, crappy technology (BAT files) and b) asking what seems to be a redundant question. I'm limited in the technology I'm allowed to use in this particular case and after looking at dozens of posts on the subject I can't find anything I can adapt to what I need.
I have a directory structure that looks like this:
A
B
C
D
etc...
XYZ
more folders
My BAT file is located outside this files system. I need to inspect it starting at level "C" and need to find the "XYZ" directory. The folders between C and XYZ can have variable names depending on the environment in which the files were created. I need to end up with a string that consists of the directory names from C through XYZ (i.e. "C\D\E\F....\XYZ") that I can put into a variable so when my BAT file is completed I can reference the variable and run another command.
I've looked at posts using FIND and FOR but I can't seem to figure out how to a) limit the string to the starting directory (for example when I combine FOR with DIR I get "A\B\C...") and how to stop when I get to "XYZ"...
Any help is greatly appreciated.

This should work in most situations:
#echo off
setlocal enableDelayedExpansion
set "root=c:\a\b\c"
set "target=xyz"
for %%R in ("%root%") do for /f "delims=" %%F in (
'dir /b /s /ad "%root%\%target%"'
) do (
set "fullPath=%%F"
set "relpath=!fullPath:%%~dpR=!"
)
echo !relpath!
It can fail if any of your paths contain ! or =. There are solutions for this, but the code is significantly more complicated.
EDIT
Actually, there is a relatively simple solution using FORFILES that should work in all situations. (Assuming your version of Windows has FORFILES)
#echo off
setlocal disableDelayedExpansion
set "root=c:\a\b\c"
set "target=xyz"
for /f "delims=" %%F in (
'forfiles /p "%root%" /m "%target%" /s /c "cmd /c if #isdir==TRUE echo #relpath"'
) do set "relpath=%%~F"
for %%R in ("%root%") do set "relpath=%%~nxR%relpath:~1%"
echo %relpath%
The only restriction is the code has to change slightly if your result contains poison characters like &. In that case you need to add quotes to the final ECHO statement, or else enable delayed expansion at the end and use echo !relpath!

For a) question:
FOR /F "TOKENS=*" %%d IN ('DIR A\B\C\XYZ /S /AD /B') DO SET variable=%%d
For a) and b) question:
FOR /D /R "A\B\C" %%d IN (*.*) DO IF /I "%%~nxd"=="XYZ" (SET variable=%%d& GOTO :EOF)
but this will exit batch script, so you need:
... your batch code
CALL :GET_XYZ
... your batch code
GOTO :EOF
:GET_XYZ
FOR /D /R "A\B\C" %%d IN (*.*) DO IF /I "%%~nxd"=="XYZ" (SET variable=%%d& GOTO :EOF)
ECHO XYZ not found!
GOTO :EOF

Related

Windows Batch File - Display All Folder & Sub-Folder relative paths

I am trying to have a batch file output ONLY the short-name of folders and sub-folders to a file. However, with the example below I am only able to get the fullpath.
DIR /S /B /O:N /A:D > FolderList.txt
Which will output:
X:\Example\Folder 1
X:\Example\Folder 2
X:\Example\Folder 3
X:\Example\Folder 1\Sub-Folder 1
X:\Example\Folder 1\Sub-Folder 2
X:\Example\Folder 2\Sub-Folder 1
When the desired output is:
Folder 1
Folder 2
Folder 3
Folder 1\Sub-Folder 1
Folder 1\Sub-Folder 2
Folder 2\Sub-Folder 1
The issue comes with the /S switch that allows the DIR command to recurse into sub-folders.
Is there a simple way, using only a Windows Batch File, to output a list of folders and sub-folders in the current directory to a text file?
#ECHO OFF
FOR /f "delims=" %%a IN ('DIR /S /B /O:N /A:D') DO (
SET "name=%%a"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(!name:%cd%\=!
endlocal
)
GOTO :EOF
made a little more confusing because you ask for the "short name" then describe the "relative name".
Obviously, name simply echoed to screen. You're aware of how to redirect.
Just to include a solution using a recursive function call approach
#echo off
setlocal enableextensions disabledelayedexpansion
call :getSubFolderList "%~1"
goto :eof
:getSubFolderList folder [prefix]
for /d %%a in ("%~f1.\*") do for %%b in ("%~2%%~nxa") do (
echo %%~b
call :getSubFolderList "%%~fa" "%%~b\"
)
goto :eof
When called with a starting folder, it will iterate over the list of subfolders, and for each one the subroutine will call itself to handle the descending folders.
The other answers to this question using echo to output the folder path use delayedexpansion, echoing the content of the variable used without any problem caused by the parser. But the code in this answer does not use delayed expansion, and characters as &() in the folder names can be a problem in the echo command.
To avoid this problem, the value to echo is quoted and wrapped inside a for replaceable parameter. That is the reason for the for %%b in the code.
To generate a file with the list of folders, the only change needed is, in the first call command:
call :getSubFolderList "%~1" > fileList.txt
note: the code in the answer has been written to receive the folder to be processed as the first argument to the batch file. Change "%~1" to your needs.
This uses a little trick with the old SUBST command to make the root folder a drive letter. Then you can use the FOR command modifiers to drop the drive letter from the variable on output.
#echo off
SET "folders=X:\Example"
subst B: "%folders%"
B:
(FOR /F "delims=" %%G in ('DIR /S /B /O:N /A:D') do echo %%~pnxG)>"%~dp0folderlist.txt
subst B: /D
Now obviously this does not check to make sure that the drive letter B: is available before using it. Easy enough to add some code to check for a drive letter that is not in use before executing the SUBST command.
To confirm my comment with regard the duplicate response:
#Echo Off
SetLocal EnableDelayedExpansion
For /L %%A In (1 1 8192) Do If "!__CD__:~%%A,1!" NEq "" Set/A "Len=%%A+1"
SetLocal DisableDelayedExpansion
(For /D /R %%A In (*) Do (
Set "AbsPath=%%A"
SetLocal EnableDelayedExpansion
Echo(!AbsPath:~%Len%!
EndLocal))>FolderList.txt

Replace "." to "_" in folders using CMD

As can be seen in the image I have folders with "." in them I would like to replace these with a "_" using CMD is there a method to do this.
cmd.exe shell scripting is the worst approach for anything more than #echo off :-)
But ok.
You can use the enhanced shell command set to replace characters in a variable:
set DUH=FBB
echo %DUH:B=O% -> FOO
So, for your problem, you need to read all folders and get them in a variable, so you can replace .=_ and then rename.
First batch: rena.cmd iterates over your folders
#echo off
for /D %%i in ( *.* ) do call rena2.cmd %%i
Second batch: rena2.cmd handles the rename
#echo off
setlocal enableextensions
setlocal enabledelayedexpansion
set TONAME=%~1
move %1 "%TONAME:.=_%"
exit /B
This can be done in one script, feel free to fiddle it together, I won't :-)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\*.*" '
) DO (
SET "dirname=%%a"
SET "dirname=!dirname:.=_!"
IF "!dirname!" neq "%%a" ECHO(REN "%sourcedir%\%%a" "!dirname!"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Dimply perform a directory-list, change the . to _ and if a change was made, perform the rename.

Keep X amount of files in folder, forfiles

I would like to keep the X latest files from a folder and delete the rest. Is this possible with FORFILES? If it's not I can fallback to another solution I seen here. Thanks for help.
I did this but it takes by dates: EDIT
forfiles /p [path] /s /d -5 /c "cmd /c echo #file"
(echo file for testing purpose)
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET /a retain=10
FOR /f "skip=%retain%delims=" %%a IN (
'dir /b /a-d /o-d "%targetdir%\*" '
) DO ECHO (DEL "%targetdir%\%%a"
GOTO :EOF
You would need to change the setting of targetdir to suit your circumstances. Equally, this procedure targets all files - change the filemask to suit.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
method is to simply execute a dir in basic form without directories, sorted in reverse-date order.
Skip the first 10 entries, and delete the rest.
With forfiles I see no chance to accomplish your task of returning the newest (most recent) number of files.
So my idea for this approach is this:
to use dir /B /A:-D /T:C /O:-D to retrieve a bare list (/B) of files (no directories, /A:-D), sorted by creation date (/T:C; if you want to use the last modification date, simply remove the /T:C portion) in decending order (/O:-D), meaning newest items first;
to put over a for /F "eol=| delims=" loop to gather and parse the dir output line by line, meaning file by file, not excluding file names beginning with ; (eol=|, | is illegal for file names) and not splitting file names containing white-spaces like SPACE or TAB (delims=);
to establish a variable that constitutes a counter, incremented per each loop iteration;
to place an if condition inside of the loop to check if the counter reached the desired limit number and in case it is fulfilled, to break the for /F loop by goto;
Here is the related code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define global constants here:
set "TARGETPATH=\path\to\files\*.*"
set /A "TOTAL=10"
set /A "COUNT=0"
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /T:C /O:-D "%TARGETPATH%"
') do (
echo(%%F
set /A COUNT+=1
setlocal EnableDelayedExpansion
if !COUNT! GEQ %TOTAL% (
endlocal
goto :NEXT
) else (
endlocal
)
)
:NEXT
endlocal
exit /B
I toggled the delayed variable expansion within the for /F loop to avoid trouble in case file names contain exclamation marks !, which would get lost in the line echo(%%F in case it is on.
Update
The following code accomplishes the original task of your question, namely to delete files in a given directory but to keep the most recent number of files:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define global constants here:
set "TARGETPATH=\path\to\files\*.*"
set /A "TOTAL=10"
set "SKIPOPT=" & if %TOTAL% GTR 0 set "SKIPOPT=skip=%TOTAL% "
for /F "%SKIPOPT%eol=| delims=" %%F in ('
dir /B /A:-D /T:C /O:-D "%TARGETPATH%"
') do (
del /P "%%F"
)
endlocal
exit /B
Since for /F supports a skip= to skip the given number of lines, and so files in our situation, let us make use of it. It is given indirectly here via variable SKIPOPT, which holds the entire option string like skip=10 (given that TOTAL is set to 10). The if %TOTAL% GTR 0 query is implemented for the script not to fail in case TOTAL is 0, because for /F does not accept the option skip=0.
The /P switch at the del command lets appear a prompt Delete (Y/N)? for testing purposes. If you do not want any prompts, simply remove it.

Script to count files in subdirs based on reoccurring subdir

I'm lookiong for a batch script which let's me count only the amount of files in a certain sub dir.
I have a directory tree with various projects (300+) and I am only looking for the amount of files in a reocurring subdirectory.
Currently I am using mtee(small program to revert cmd output to a txt file) and dir to count the subdirs and do a manual search in excel.
I was wondering if there is a way to do this in with a batch script.
e.g: Every project has the directory proposals and subdirectory no-go proposals. I want to count the amount of files in the no-go proposals from the basis of the directorytree
for /f %%i in ('dir /s/b/a-d "c:\project"^|find /c "\no-go-dirname\"') do ECHO nogocount=%%i
Well, that's the line within a batch file. If you're running it from the prompt, each %% becomes %
do a directory list of all of the filenames, /a-d not directorynames, /s including subdirectories /b in basic form, so no headers or trailers. Filter this with a FIND and /c count the lines that contain the string "\no-go-dirname\" and assign the count to %%i
Try this:
#echo off &setlocal
set "startfolder=x:\proposals\no-go proposals"
set /a counter=0
for /r "%startfolder%" %%i in (*) do set /a counter+=1
echo %counter% files found in "no-go proposals" .
Read HELP FOR and HELP SET and try the following code
#echo off
setlocal enabledelayedexpansion
for /r /d %%d in (*) do (
call :countf "%%d\nogo"
)
goto :eof
:countf
set count=0
for %%f in ("%~1\*") do (
set /a count+=1
)
echo %~1 has !count! files
goto :eof
Good to see so many alternatives, and all of them works seemlessly however this is my version.
dir /s/b /a-d | find /c /v ""
running this command in a directory where you need the file count will return just the count.

Amending text file strings and using the result to perform an xcopy

I have a windows boot pen that runs a batch file when it starts up, all it needs to do is copy a large list of files specified in a text file from the machine to the boot pen.
I made a test run on my PC before making the boot pen and thought this should work
#echo off
set DRIVE=c
for /F "tokens=*" %%a in (e:\test\files.txt) do call :amendDirectoryAndCopy %%a
pause
:amendDirectoryAndCopy
set DEST=%~1
set DEST=%DEST:~1%
echo set DEST=%DRIVE%%DEST%
echo xcopy %~1 %DEST%
all it should do is for each file, remove the first character of the string, add "c" to the beginning which gives the destination directory, then perform an xcopy. I find the output confusing as "#echo set "DEST=%DRIVE%%DEST%" outputs what I would expect, the correct directory on C: such as
c:\test\folder\file.txt
but the xcopy outputs
xcopy e:\test\folder\file.txt :\test\folder\file.txt
the drive letter is missing on the destination.
I believe SetLocal EnableDelayedExpansion is needed along with its counterpart ! replacement of % in variable expansion to get your code to work.
I am away from my Windows machine right now, so I cannot test the syntax, but off the top of my head, something like this should work:
SetLocal EnableDelayedExpansion
#echo off
set drive=c
set ready_to_move=0
set file_list=e:\test\files.txt
if "!ready_to_move!" == "1" (
set echo=
) else (
set echo=echo
)
for /F "eol=; tokens=1 delims=" %%f in ('type "!file_list!"') do (
set source=%%~f
for /f "tokens=* delims= " %%s in ("!source!") do set source=%%s
set destination=!drive!!source:~1!
!echo! xcopy "!source!" "!destination!"
)
Does this work for you?

Resources