Iterate Folder Name in batch Files - batch-file

Currently I am iterating all files in a folder, currently I am in Sample folder and I am doing it like this:
#echo off
for /R %%a in ("*") do (
#echo %%a
)
pause
And it lists files like this:
C:\Users\-----\Desktop\Sample\mybat.bat
C:\Users\-----\Desktop\Sample\bin\1.dll
C:\Users\-----\Desktop\Sample\content\themes\2.css
What I want is to list the name but starting from current folder where I put the .bat file, i. e., it should look like this:
Sample\mybat.bat
Sample\bin\1.dll
Sample\content\themes\2.css

A nice way to return a list of files relative from the current directory is to use xcopy /L:
pushd "%~dp0.."
xcopy /L /S /I ".\Sample" "%TEMP%" | find ".\"
popd
To loop through all the returned items, use this:
pushd "%~dp0.."
for /F "delims=" %%F in ('
xcopy /L /S /I ".\Sample" "%TEMP%" ^| find ".\"
') do (
echo(%%F
)
popd
To remove the .\ prefix, use the following:
pushd "%~dp0.."
for /F "tokens=1* delims=\" %%E in ('
xcopy /L /S /I ".\Sample" "%TEMP%" ^| find ".\"
') do (
echo(%%F
)
popd

Related

Batch file relative path to current dir

I'm doing some BATCH scripting looping through files to copy. But I came to a problem where I need the path relative to the current .bat execution folder (%cd%)
So if I have files like this:
c:\games\batchTest\test.bat
c:\games\batchTest\subFolder1\test1.txt
How can I get just "subFolder1\test1.txt" so I can copy the file with the sub folder?
My current code:
for /r %%a in (*) do (
echo "%%a"
)
You can try this:
#Echo Off
Setlocal enabledelayedexpansion
For /r %%a In (*) Do (
Set p="%%a"
Echo !p:%__CD__%=!
)
The for /R loop always returns absolute paths, even if the (optional) given root directory behind /R is relative.
A possible way to get relative paths is to (mis-)use the xcopy command together with its /L option that prevents anything to be copied:
xcopy /L /S /I ".\*.*" "%TEMP%"
To remove the summary line # File(s) apply a filter using find using a pipe:
xcopy /L /S /I ".\*.*" "%TEMP%" | find ".\"
To process the returned items use a for /F loop:
for /F "eol=| delims=" %%F in ('
xcopy /L /S /I ".\*.*" "%TEMP%" ^| find ".\"
') do (
echo Processing file "%%F"...
)
If you just want to copy files including the sub-directory structure you do not even need the above stuff with for loops, you can simply use xcopy:
xcopy /S /I "D:\Source\*.*" "D:\Destination"
or robocopy:
robocopy /S "D:\Source" "D:\Destination" "*.*"

Batch to find a folder in subfolders, move files one level up and delete that folder

I have this structure:
..
..\FolderA\FolderX\File1.txt
..\FolderB\FolderX\File2.txt
..\FolderC\FolderD\FolderE\FolderX\File3.txt
I need a batch to find all "FolderX" in subfolders and move all the files in "FolderX" to one level up and delete that "FolderX"
..
..\FolderA\File1.txt
..\FolderB\File2.txt
..\FolderC\FolderD\FolderE\File3.txt
How write a batch? I tried this, but is incomplete, the code not find the folders:
#Echo Off
Set _Source=%~dp0
Set _FindDir=FolderX
Set _Path=%_Source%\%_FindDir%
If Exist "%_Path%" (
Move /-Y "%_Path%\*.*" "%_Source%"
For /F "Tokens=* Delims=" %%I In ('Dir /AD /B "%_Path%"') Do Move "%_Path%\%%I" "%_Source%"
RD /S /Q "%_Path%"
) Else (
Echo There is no %_FindDir% folder in %_Source%
)
Resolved:
#echo off & setlocal enabledelayedexpansion
for /d /r %~dp0 %%a in (*) do (
if /i "%%~nxa"=="FolderX" (
set "folderpath=%%a" (
move /y !folderpath!\* !folderpath:~,-8!
rmdir !folderpath!
)
)
)

Windows Command to identify folders with only one file

I need a command to run from the Windows CLI to identify any folders (or sub folders) that contain only one file. If the folder contains two files, it should not be included. In the end, I need to output this list to a text file. It should contain the full folder path.
Ex: OutputLog.txt
C:\fold1
C:\fold1\sub
C:\fold3
C:\fold4
This should work to identify folders with one file.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
dir /b /a-d "%%a" 2>nul |find /c /v "" |findstr "^1$" >nul && >>file.txt echo %%a
)
#echo off
setlocal EnableDelayedExpansion
(for /D /R %%a in (*) do (
set count=0
for %%b in ("%%a\*.*") do set /A count+=1
if !count! equ 1 echo %%a
)) > OutputLog.txt
#echo off
set "parentfolder=c:\test"
for /f "tokens=* delims=" %%F in ('dir /s /a:d /b "%parentfolder%"') do (
dir "%%F"|findstr /b "1 File(s)" >nul 2>&1 && echo %%F
)
This will list all subfolders with only one file in a parent folder .As it checks the string of the dir command output it should be altered if language settings/windows version provide different DIR command output.

Batch Search combo Z: Test List

for /f "tokens=* delims=" %%a in ('dir "recordings.txt" /s /b') do (
echo %%a
)
Is this the correct format to look on the Z: drive for files in the recordings.txt?
Tried powershell don't have permissions on the server
Contents of the file just have the file name / extension
3030009948_3030009912_df1389947f0fb80d62832122.sasf
The Directory structure of Z: is as follows
MM\dd\hh\mm\
recordings.txt is on the Desktop of my userprofile
I also need the paths of the found files
This should read recordings.txt on your desktop and create recordings-results.txt in the same place with the full path to every file inside it.
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
del "%userprofile%\desktop\recordings-results.txt" 2>nul
for /f "usebackq delims=" %%a in ("%userprofile%\desktop\recordings.txt") do (
echo finding "%%a"
findstr /i /c:"%%a" "%temp%\results.tmp" >>"%userprofile%\desktop\recordings-results.txt"
)
del "%temp%\results.tmp"
pause
This should be even faster:
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
findstr /i /g:"%userprofile%\desktop\recordings.txt" "%temp%\results.tmp" >"%userprofile%\desktop\recordings-results.txt"
del "%temp%\results.tmp"
pause
Dir is to look into a directory if you want to get a value from a file use :
for /f "tokens=*" %%a in ('type "Z:\recordings.txt"') do (
echo %%a
)

Batch: find all files with exclude part of the name

I need to find all * .avi files (even in subfolders). But I want to exclude files with specific part of the name (* demux.avi). Something like this:
FOR /R %%g IN (*.avi) DO (
if "%%g"=="*demux.avi" ( - THIS PART IS WRONG
echo "%%g" excluded
) else (
echo "%%g"
)
)
I expect results:
file1.avi
file1 demux.avi excluded
file2.avi
**
Try this:
FOR /F %%g IN ('dir /b /s "*.avi"^|find /i /v "demux"') DO ( whatever )
We are basically saying look for all avi files in this folder and subfolders and pipe them to find.
Use finds /V switch that finds all lines NOT containing "demux" and run your command on them.
Like this :
#echo off&cls
for /f "delims=" %%a in ('dir /b/a-d *.avi') do (echo %%a | find "demux" && echo %%a [Excluded] || echo %%a [OK])
In place of echo %%a [Excluded] or echo %%a [OK] you can do what you want (DEL, REN,MOVE,...)
Edit :
To just have the name of the file when your using /S option of DIR
#echo off&cls
for /f "delims=" %%a in ('dir /s/b/a-d *.avi') do (echo %%~na | find "demux" && echo %%~na [Excluded] || echo %%~na [OK])

Resources