Write batch file to count folders inside main folder - windows

I am new to this, i am trying to write a batch file to calculate number of folders inside folder. Can someone please help me?
Here is folder system:
I have hundreds of folders like:
Area1
Area2
Area3
....
Inside each of above folder there is one folder named "Zone".
What i am trying to find is number of folders inside Zone folder for each Area1, Area2... so on.

start one folder above the AREAx folders:
#echo off &setlocal
for /d %%i in (*.*) do (
pushd %%i\ZONE
set /a count=0
for /d %%j in (*.*) do set /a count+=1
popd
call echo %%count%% folder(s^) in %%i\ZONE
)
endlocal

#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f %%i IN ('dir /b/ad area*') DO (
IF EXIST ".\%%i\zone\." (
FOR /f %%c IN (' DIR /ad ".\%%i\zone\"') DO SET /a count=%%c-2 2>nul
ECHO ".\%%i\zone" : !count! directories
) ELSE (ECHO ".\%%i\zone" does NOT EXIST
)
)

Related

Batch file to arrange files per name into relevant folders

I have the following folders:
Apple_folder
Pear_folder
Tomatoes_folder
Within Apple_folder, I have the following files:
Extracted-Apple_1.txt
Extracted-Apple_2.txt
Extracted-Pear_1.txt
Extracted-Pear_2.txt
Extracted-Apple_3.txt
Extracted-Tomatoes_1.txt
How do I move Extracted-Pear_1.txt, Extracted-Pear_2.txt, Extracted-Tomatoes_1.txt into their relevant folders, i.e. keep file name that contain apple under Apple_folder, etc.
Expected Apple_folder contents:
Extracted-Apple_1.txt
Extracted-Apple_2.txt
Extracted-Apple_3.txt
Expected Pear_folder contents:
Extracted-Pear_1.txt
Extracted-Pear_2.txt
Expected Tomatoes_folder contents:
Extracted-Tomatoes_1.txt
I am still a beginner in batch script, so not sure how to proceed?
The following script didn't work.
setlocal enabledelayedexpansion
set /A counter=0
#echo off
for %%a in (*.txt) do (
for /f "tokens=2 delims=-_" %%f in ("%%a") do (
set /A counter+=1
MOVE "%%a" "C:\Users\ADMIN\Documents\TESTING\Batch_script\%%f_folder\Extracted-%%~nf_moved__!counter!.txt"
)
)
pause
I got to the bottom of this. This is the solution I was looking for. It will recognise the file names and check whether they're under the right folder, if not it will move them under the right folder:
setlocal enabledelayedexpansion
for /R "C:\Users\Admin\Documents\TESTING\Batch_script\" %%G IN (*.txt) do (
for /F "tokens=7,12,13 delims=\-_." %%a in ("!%%G!") do (
set /A counter+=1
IF NOT %%a==%%b (MOVE "%%G" "C:\Users\ADMIN\Documents\TESTING\Batch_script\%%b_folder\MSG-Extracted-%%b_%%c_moved_!counter!.txt")
)
)

Batch file to search directory for list of files and copy to single folder

I have a .txt file with a list of files that are on a network drive (M:). I need a batch file to go through that list, search for the files which are in sub-directories of a single folder on that drive, and then copy them to another folder. I have tried quite a few solutions with no luck.
the text file is a list of files and extensions i.e.
abc.step
afer.iges
ca76dc7d.sldprt
Here's what I tried so far
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cls
set dest=C:\Users\kduquette.000\Desktop\Files
for /f "TOKENS=*" %%f in (C:Desktop\Files\list.txt) do (
set i=1
for /f "tokens=*" %%F IN ('dir M: "%%~f"') Do (
for %%N in ("%%F") Do (
set name=%%~NN
set ext=%%~XN
)
copy "%%~F" "%dest%\!name!_!i!!ext!"
set /A i=!i!+1
)
)
ENDLOCAL
We can just do a check if file exists and copy it:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do if exist "M:\Cads\%%i" echo copy "M:\Cads\%%i" "%dest%"
Note, for now, I added echo to the copy string, so you can test the result first. Once happy with the results, remove echo
If however you want to search for the files, recursively throughout M:\ drive then:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do (
pushd M:\
for /f "delims=" %%a in ('dir /b /s "%%i"') do echo copy "%%~fa" "%dest%"
popd
)

Batch script file to copy and rename files

I'm writing batch script which I'll use to copy files from location A to location B with rename of a source file from location A if the same file exists already in location B.
Currently Im using snippet from another topic here on stack but it doesnt work on files from subfolders, could anyone help me with code below so it work on all files and subdirectiories from both locations? Many thanks!
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET sourcedir="D:\TEST\FROM"
SET destdir="D:\TEST\TO"
SET /a count=0
for %%c in (%sourcedir%\*.*) do (
CALL :select
ECHO copy "%%c" "%destdir%\%%~nc_!count!%%~xc" /s
)
GOTO :EOF
:select
SET /a count+=1
IF EXIST "%destdir%\%%c" GOTO select
GOTO :eof
Replace your for loop with the following for loop:
for /R "%sourcedir%" %%c in (*.*) do (what you like)
Also, why do you want the following piece of code?
copy "%%c" "%destdir%\%%~nc_!count!%%~xc" /s
Just copy "%%c" %destdir%
More generally you can write:
#ECHO OFF
SET sourcedir="D:\TEST\FROM"
SET destdir="D:\TEST\TO"
:: SET /a count=0
for /R "%sourcedir%" %%c in (*.*) do (
:: SET /a count+=1
IF NOT EXIST "%destdir%\%%c" (
echo copy "%%c" %destdir%
)
)
Hope you are fine with this, possible dublicate of Windows batch file with loop through subfolders
Sharing with what I managed to get so far, works for what I needed, however still not doing well for subfolders:
#ECHO OFF
SET "sourcedir= "
SET "destdir= "
SET "HH=%TIME:~0,2%"
SET "MM=%TIME:~3,2%"
SET "SS=%TIME:~6,2%"
SET "_Time=%HH%%MM%%SS%"
FOR /R "%sourcedir%" %%G IN (*.*) DO (
IF EXIST "%destdir%\%%~nG%%~xG" (
COPY /V /Z "%%G" "%destdir%\%%~nG_duplicate_%_Time%%%~xG"
) ELSE (
COPY /V /Z "%%G" "%destdir%\%%~nG%%~xG")
)

Batch file to create directories and move files based on partial file names

I have a bunch of files which have names like the following:
BS01_1234_1234.jpg
BS01_1235_6789.jpg
TP01_1234_6879.jpg
All kept in a local C drive folder.
I'm hoping to move these to a networked folder \\autrsrv02\Tempdata\Justine Tennent\movetofolder but grouped into subfolders based on the first 9 characters of their file name, BS01_1234.
I have the following so far but no luck.
this code worked to move a file:
move /-y "C:\Users\jtennent\Documents\testbatchfile\**.jpg" "\\autrsrv02\Tempdata\Justine Tennent\movetofolder"
pause
but i tried this to create sub folders and move with no luck
#ECHO OFF
SETLOCAL
SET "sourcedir=*C:\Users\jtennent\Documents\testbatchfile*"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=_" %%a IN (
'dir /b /a-d "*_*.jpg"'
) DO (
MD %%a_%%b 2>nul
MOVE "%%a_%%b_%%c_%%d" .\%%a_%%b\ >nul
)
POPD
GOTO :EOF
Any help would be appreciated.
Edit
The following groups but splits too early
#echo off
for %%A in (.psd *.jpg) do (
for /f "tokens=1 delims=_" %%D in ("%%~nA") do (
md "%%D" 2>nul
echo Moving file %%A to folder %%D
move "%%A" "%%D" >nul
)
)
echo Finished
pause
Below is some untested example code, (it should move the file to the new location also removing the first ten characters from the file name in the new location)
#Echo off
Set "SrcDir=%UserProfile%\Documents\testbatchfile"
Set "DstDir=\\autrsrv02\Tempdata\Justine Tennent\movetofolder"
If Not Exist "%DstDir%\" Exit/B
If /I Not "%CD%"=="%SrcDir%" PushD "%SrcDir%" 2>Nul||Exit/B
For /F "EOL=_ Tokens=1,2* Delims=_" %%A In (
'Where .:"????_????_*.psd" .:"????_????_*.jpg"') Do (
If Not Exist "%DstDir%\%%A_%%B\" MD "%DstDir%\%%A_%%B"
Echo Moving "%%A_%%B_%%C" to "%DstDir%\%%A_%%B\%%C"
Rem Move /-Y "%%A_%%B_%%C" "%DstDir%\%%A_%%B\%%C")
Echo Finished
Pause
Change the locations in lines 2 & 3 to suit your requirements. If you're happy with the output change line 12 to:
Move /-Y "%%A_%%B_%%C" "%DstDir%\%%A_%%B\%%C">Nul)
and invoke the script again.

Batch rename files with the folder name and sequence number - Send To Menu

I have a folder called sales. I have imgxy.jpg, imgab.jpg etc in that folder. When I right click the sales folder and send to rename.bat file (I have already copied the rename.bat file into c:\documents and settings\username\send To) I want the files in the sales folder to change to sales1.jpg, sales2.jpg etc. This behaviour should be true for anything I send to this .bat file. The following code was working properly on windows 7 last night. Now I am om windows xp sp3 and it doest work. It is trying to rename files in the folder *c:\documents and settings\username*
Please help rename.bat file code below
#ECHO OFF
setlocal enabledelayedexpansion
set foldername=%1
for %%i in (%bar%) do set bar=%%~ni
FOR /D %%# in (%bar%) DO (
PUSHD "%%#"
FOR %%# in ("*") DO (
set /a "inc+=1"
Echo Ren: ".\%%~n#\%%#" "%%~n#!inc!%%~x#"
Ren "%%#" "%%~n#!inc!%%~x#"
)
POPD
)
Test this - it accepts a folder, not files, but you mentioned folders in your question.
Don't call it rename.bat because rename is an internal command.
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=0
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1!inc!%%~xa"
Ren "%%a" "%~n1!inc!%%~xa"
)
POPD

Categories

Resources