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")
)
)
Related
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")
)
#echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.jpg) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.jpg
I have this batch file to rename all files in folder as 1,2,3...n But the problem is its removing the caption, I want to modify it such that it will keep the caption as it is and will just add a number before a caption.
#Echo Off
SetLocal EnableDelayedExpansion
Set "i=0"
For %%A In (*.jpg) do (Set /A i+=1
Ren "%%A" "!i!%%~nA.new"
)
Ren *.new *.jpg
The filename plus extension above is %%A and the filename without extension is %%~nA. So I used Ren "%%A" "!i!%%~nA.new". Please refer to the help usage of the For command for a full explanation, enter For /? at the Command Prompt to do so.
You can separate the !i! from the %%~nA with a character of your choosing too!
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.
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
)
)
I have and will have files which are named "x_1.txt x_2.txt x_3.txt, ..." my other program where I input these files cannot recognize the order so it sorts like this "x_1.txt , x_101.txt , x_2.txt"). a solution is to rename the files to x00001.txt , x00002.txt , ....
I have so far wrote the .bat file below, but two problems I have which , I'd be very glad if you help me solve them :
1- how can I remove the 'number'.txt from string x_'number'.txt
2- (solved) how can I use the variable of this string to rename the file name ( the rename part of this file is not working!)
cls
setlocal enabledelayedexpansion
set /A count=100000
for %%f in (*.txt) do (
set /a count+=1
set str=!count:~1!
echo !str!
echo %%f
set filename=%%f
set filename=!filename:~0,5! /Comment: here I want to just keep the x_ part which I don't know how"
echo !filename!
set str3=!filname!!str!
echo !str3!
/// ren %%f !str3!.txt /Comment: Here I cannot use the variable str3,
call:renamer %%f !str3!
)
:renamer
ren %1 %2.txt
Thanks in advance
If the following conditions are true:
You want to rename all of your .txt files in the current folder
All of the .txt files have exactly one _ in the name, immediately before the number
None of your file names contain !
Then the following will work
#echo off
setlocal enableDelayedExpansion
for %%F in (*.txt) do for /f "tokens=1,2 delims=_." %%A in ("%%F") do (
set num=0000%%B
ren "%%F" "%%A!num:~-5!.txt"
)
But to eliminate the conditions requires much more complicated code.
Here is one robust solution that should properly rename all files that meet the template.
It allows for multiple _ in the name.
It only renames files with a name that ends with _NNN.txt where NNN is a number
It properly handles ! in the file name.
Note that it will not properly handle numbers that exceeds 99999. It is simple to expand the degree of 0 padding.
#echo off
setlocal disableDelayedExpansion
pushd .
subst #: .
#:
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt^|findstr /er "_[0-9]*.txt"') do (
set "name=%%~nF"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!name:_=\x!") do (
endlocal
set "file=%%F"
set "name=%%~pA"
set "num=%%~nA"
setlocal enableDelayedExpansion
set "num=0000!num:~1!"
set "name=!name:~1,-1!"
ren "!file!" "!name:\x=_!!num:~-5!.txt"
endlocal
)
)
popd
subst /d #: