I am trying to write a batch script that gets two recent files based on creation time from a given directory. In this batch script i want to invoke an .exe file that takes these two files as arguments.
Can someone please help me with this.
dir /a:-d-s /b /o:-d /t:c
This is the command i used that would list the names of the files in descending order based on the creation time. The command prints the result to the console but i want to assign it to some variable or array and access the top two filenames.
The following example is intended to set the two topmost returned files as 'array type' variables %Newest[1]% and %Newest[2]%
#For /F "Delims==" %%A In ('Set Newest[ 2^>NUL')Do #Set "%%A="
#For /F "Tokens=1*Delims=:" %%A In ('Dir /B/A-D-S/O-D/TC 2^>NUL^|FindStr /N "^"'
)Do #If %%A LEq 2 (Set "Newest[%%A]=%%B")Else GoTo Next
:Next
#Set Newest[ 2>NUL&&Pause
The last line was included just to show you any variables created from the loop. You would obviously replace this line with your own code, or leave it as is, and place your code beneath it. If you want to use the most recently modified date and time stamps instead of created, replace /TC with /TW on line 2
Off-topic:
Here's an example of it in use, based upon your later posted question, since deleted:
#Echo Off
For /F "Delims==" %%A In ('Set Newest 2^>NUL')Do Set "%%A="
For /F "Tokens=1-2*Delims=:" %%A In ('Dir /B/A-D-S/O-D/TC "Checkpoints" 2^>NUL^
^|FindStr /N "^"')Do If %%A LEq 2 (Set "Newest%%A=%%B")Else GoTo Next
:Next
Set "DT1="&For /F "Tokens=1*Delims=_" %%A In ('Set Newest 2^>NUL'
)Do If Not Defined DT1 (Set "DT1=%%~nB")Else Set "DT2=%%~nB"
If Not Defined DT1 Exit /B
Set "Target=Differences\Difference_%DT1%_%DT2%.csv"
Set Newest 2>NUL
Set Target 2>NUL
If Not Exist "Differences\" MD "Differences"
Pause
Here is a commented batch file for this task.
#echo off
rem Set up a local environment for this batch file with enabled command
rem extensions as required for command FOR and with disabled delayed
rem environment variable expansion to process also correct file names
rem with one or more exclamation marks in name.
setlocal EnableExtensions DisableDelayedExpansion
rem Make directory of the batch file the current directory.
rem It is possible to use any other directory on replacing
rem %~dp0 by a directory path.
pushd "%~dp0"
rem Make sure the environment variable File1 is not defined.
set "File1="
rem Get a list of files without system attribute in current directory
rem ordered by creation date with newest first and oldest last and process
rem this list of file names line by line with ignoring the batch file. The
rem loop processing the file names is exited after having assigned second
rem newest created file in this directory to the environment variable File2
rem in addition to newest file assigned to environment variable File1 with
rem a jump to the command line below the line with label RunProgram.
for /F "eol=| delims=" %%I in ('dir /A-D-S /B /O-D /TC 2^>nul') do (
if not "%%I" == "%~nx0" (
if not defined File1 (
set "File1=%%I"
) else (
set "File2=%%I"
goto RunProgram
)
)
)
rem There is no file or just one file found in current directory other
rem than this batch file and so the program cannot be executed at all.
goto EndBatch
rem Run the program with the file names of the two newest
rem created files in current directory (batch file directory).
:RunProgram
"C:\Path To Application\Application.exe" "%File1%" "%File2%"
rem Restore the initial current directory and also the initial environment
rem variables list on starting of this batch file as well as initial state
rem of command extensions and delayed environment variable expansion.
:EndBatch
popd
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 (drive and path of batch file) and %~nx0 (name and extension of batch file).
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
popd /?
pushd /?
rem /?
set /?
setlocal /?
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the specified command line appended.
Related
So I'm working on a script for file renaming/editing and I'm getting an error
The system cannot find the file specified
I'm assuming it's because of the loop, how can I fix this
#echo off
Setlocal enabledelayedexpansion
cd /d "%~dp0"
for /f "delims=*" %%a IN ('dir /b /s /a-d "Folder1\(*)*.txt"') DO (
Set "File=%%~nxa"
Ren "%%a" "!File:(1)=(-125)!"
Ren "%%a" "!File:(2)=(-124)!"
Ren "%%a" "!File:(3)=(-121)!"
Ren "%%a" "!File:(4)=(-117)!"
Ren "%%a" "!File:(5)=(-120)!"
Ren "%%a" "!File:(6)=(-116)!"
Ren "%%a" "!File:(7)=(-115)!"
Ren "%%a" "!File:(8)=(-127)!"
Ren "%%a" "!File:(9)=(-126)!"
Ren "%%a" "!File:(10)=(-100)!"
)
Source Folder1
(1) filename.txt
(2) filename.txt
(3) filename.txt
Source Folder1 Results
(-125) filename.txt
(-124) filename.txt
(-121) filename.txt
As you can see the script does work, but I am getting the message
I also tested it with this for /f "Tokens=*" same results
There can be used the following code for this task.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0Folder1" || exit /B
for /F "delims=" %%I in ('dir "(*)*.txt" /A-D-L /B 2^>nul') do (
set "FileName=%%I"
setlocal EnableDelayedExpansion
set "NewName=!FileName:(1)=(-125)!"
set "NewName=!NewName:(2)=(-124)!"
set "NewName=!NewName:(3)=(-121)!"
set "NewName=!NewName:(4)=(-117)!"
set "NewName=!NewName:(5)=(-120)!"
set "NewName=!NewName:(6)=(-116)!"
set "NewName=!NewName:(7)=(-115)!"
set "NewName=!NewName:(8)=(-127)!"
set "NewName=!NewName:(9)=(-126)!"
set "NewName=!NewName:(10)=(-100)!"
ren "!FileName!" "!NewName!"
endlocal
)
popd
endlocal
There is first defined the required execution environment with
command echo mode turned off and
command extensions enabled and
delayed variable expansion disabled.
Next the subdirectory Folder1 of the batch file directory is made the current working directory or the batch file processing is exited if that folder does not exist at all.
Then one more Windows command process is started in background with %ComSpec% /c and the command line within ' of command FOR appended as additional arguments. There is executed with Windows installed into C:\Windows:
C:\Windows\System32\cmd.exe /c dir "(*)*.txt" /A-D-L /B 2>nul
The command DIR searches
in the current directory
for just files because of /A-D-L (attribute not directory and not link (reparse point))
with a file name matched by the wildcard pattern (*)*.txt and
outputs just the matching names without path in bare format because of /B.
It is possible that DIR does not find a matching file name in which case an error message is output which is suppressed by redirecting it from STDERR (standard error) to device NUL.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR with option /F captures all output to STDOUT (standard output) of started cmd.exe and waits for self-termination of started cmd.exe before the captured output is processed line by line.
Empty lines are ignored which do not exist here at all. There would be split up by default the file names into substrings using normal space and horizontal tab as string delimiters, analyzed if the first substring begins with a semicolon in which case the line (file name) would be also ignored while otherwise the first space/tab delimited substring is assigned to the specified loop variable I before running the commands in body of FOR.
The line (file name) splitting behavior is not wanted which is the reason for usage of the option delims= to define an empty list of string delimiters to turn off the line splitting into substrings. The default end of line character ; can be kept in this case as all file names to process start definitely with character (.
The file name without path is first assigned to the environment variable FileName which works also for file names containing one or more exclamation marks as delayed variable expansion is disabled at execution of this command line.
Next is enabled delayed variable expansion as required for the next commands. Please read this answer for details about the commands SETLOCAL and ENDLOCAL and what really happens in memory of running cmd process on using these two commands.
A series of string substitutions is done next with command SET to define the new file name based on the current file name before running just once the command REN to rename the current file to the new name.
The code posted in the question tries to rename the file multiple times. It fails on string substitution does not change anything at all because of a file cannot be renamed on new name being equal the current name. Then one REN command works on which the string substitution was successful. The other REN command fail again because of the file is already renamed and renaming it once again with original file name cannot work anymore for that reason.
The initial environment with disabled delayed variable expansion is restored last before processing the next file name.
There could be used for this task also:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0Folder1" || exit /B
for %%# in ("1 125" "2 124" "3 121" "4 117" "5 120" "6 116" "7 115" "8 127" "9 126" "10 100") do for /F "tokens=1,2" %%G in (%%#) do for %%I in ("(%%G)*.txt") do for /F "tokens=1* delims=)" %%J in ("%%I") do ren "%%I" "(-%%H)%%K"
popd
endlocal
For each pair of current and new number a string splitting is done to assign the current number to the loop variable G and the new number to the loop variable H before a FOR is used to process all files with current number (G) of which file name is assigned to loop variable I which is split up on first occurrence of a closing round bracket to get the part after first ) from entire file name with file extension assigned to the loop variable K to be able to rename the file with the new number.
There are several other solutions possible too.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /? ... explains %~dp0 ... drive and path of argument 0 which is the batch file path always ending with a backslash.
dir /?
echo /?
endlocal /?
exit /?
for /?
popd /?
pushd /?
ren /?
set /?
setlocal /?
Read also single line with multiple commands using Windows batch file for an explanation of conditional command operator || used on third command line.
The first batch file solution with processing also files in subfolders:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0Folder1" || exit /B
for /F "delims=" %%I in ('dir "(*)*.txt" /A-D-L /B /S 2^>nul') do (
set "FullName=%%I"
set "FileName=%%~nI"
setlocal EnableDelayedExpansion
set "NewName=!FileName:(1)=(-125)!"
set "NewName=!NewName:(2)=(-124)!"
set "NewName=!NewName:(3)=(-121)!"
set "NewName=!NewName:(4)=(-117)!"
set "NewName=!NewName:(5)=(-120)!"
set "NewName=!NewName:(6)=(-116)!"
set "NewName=!NewName:(7)=(-115)!"
set "NewName=!NewName:(8)=(-127)!"
set "NewName=!NewName:(9)=(-126)!"
set "NewName=!NewName:(10)=(-100)!"
ren "!FullName!" "!NewName!%%~xI"
endlocal
)
popd
endlocal
The second batch file solution with processing also files in subfolders:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0Folder1" || exit /B
for %%# in ("1 125" "2 124" "3 121" "4 117" "5 120" "6 116" "7 115" "8 127" "9 126" "10 100") do for /F "tokens=1,2" %%G in (%%#) do for /R %%I in ("(%%G)*.txt") do for /F "tokens=1* delims=)" %%J in ("%%~nxI") do ren "%%I" "(-%%H)%%K"
popd
endlocal
The text files with hidden attribute set are ignored by the FOR loop searching recursively in the subdirectory Folder1 of the batch file directory for the files with the number in round brackets assigned to the loop variable G.
::for /l %%n in (0, 1, 6) do (
for /F "skip=1 delims=" %%i in (path.txt) do set "dirvar=%%i"&goto nextline
:nextline
for /F "skip=1 delims=" %%i in (file.txt) do set "filevar=%%i"&goto nextline
:nextline
for /F "skip=1 delims=" %%i in (dotonefile.txt) do set "dotvar=%%i"&goto nextline
:nextline
SET dirvar=%dirvar%
SET filevar=%filevar%
SET dotvar=%dotvar%
SET dirfile=%dirvar%%filevar%
SET dirdotfile=%dirvar%%dotvar%
IF EXIST %dirfile% (
del %dirdotfile%
) ELSE (
rename %dirdotfile% %dirfile%
)
::)
My batch script above works fine in that it runs one time. It reads the 2nd line from three separate text files into variables. Then it tests to see if a filename is in a directory and if it is named IMG001.jpg it deletes IMG001.1.jpg. in the same directory. If IMG001.jpg is NOT found in the directory, it renames IMG001.1.jpg in the directory to IMG001.jpg.
path.txt is just a text file with a list of folder paths like:
F:\My Pictures\2005-Misc\
F:\My Pictures\2006-Misc\
F:\My Pictures\2007-Misc\
file.txt is just a text file with a list file names, where line 1 is a file in the directory that's also line 1 of the path.txt file. So there could be a IMG001.jpg in the 2005-Misc folder, could be a IMG001.jpg in the 2006-Misc folder, and there could be a IMG001.jpg in the 2007-Misc folder:
IMG001.JPG
IMG001.JPG
IMG001.JPG
Similarly with dotonefile.txt, it's a list of filenames that ARE in the corresponding directory listed in path.txt. So there IS a IMG001.1.jpg in folder 2005-Misc, there's one in 2006-Misc, and there's one in 2007-Misc.
IMG001.1.JPG
IMG001.1.JPG
IMG001.1.JPG
I want to loop this script and repeat it so it reads in lines 1 through n (n can be hard coded, above it is currently 7) from the text files to variables, then tests and renames for each filename.
I tried uncommenting the first and last lines and then in the three for loops, I replaced the hardcoded "1" with "%%n" but the batch file won't run erroring with "the sntax of the command is incorrect". Below is my attempt that doesn't work. Any advice on how to tweak it to run? I've tried all kinds of combinations of making a new count variable that increments by 1 at the end, using delayed expansion in various forms of variables, nothing works.
for /l %%n in (0, 1, 6) do (
for /F "skip=%%n delims=" %%i in (path.txt) do set "dirvar=%%i"&goto nextline
:nextline
for /F "skip=%%n delims=" %%i in (file.txt) do set "filevar=%%i"&goto nextline
:nextline
for /F "skip=%%n delims=" %%i in (dotonefile.txt) do set "dotvar=%%i"&goto nextline
:nextline
SET dirvar=%dirvar%
SET filevar=%filevar%
SET dotvar=%dotvar%
SET dirfile=%dirvar%%filevar%
SET dirdotfile=%dirvar%%dotvar%
IF EXIST %dirfile% (
del %dirdotfile%
) ELSE (
rename %dirdotfile% %dirfile%
)
)
The main problem is that Windows command processor cmd.exe does not support labels inside command blocks which are parsed completely before executing the command making use of the command block. Please read for details How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The solutions is using a subroutine.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /L %%N in (0,1,6) do call :ProcessFiles %%N
endlocal
goto :EOF
:ProcessFiles
if not %1 == 0 ( set "SkipOption=skip=%1 " ) else ( set "SkipOption=" )
set "DirVar="
for /F "%SkipOption%eol=| delims=" %%I in (path.txt) do set "DirVar=%%I" & goto GetFileVar
:GetFileVar
set "FileVar="
for /F "%SkipOption%eol=| delims=" %%I in (file.txt) do set "FileVar=%%I" & goto GetDotVar
:GetDotVar
set "DotVar="
for /F "%SkipOption%eol=| delims=" %%I in (dotonefile.txt) do set "DotVar=%%I" & goto CheckFile
:CheckFile
set "DirFile=%DirVar%%FileVar%"
set "DirDotFile=%DirVar%%DotVar%"
if exist "%DirFile%" (
del "%DirDotFile%"
) else (
rename "%DirDotFile%" "%DirFile%"
)
goto :EOF
A smarter approach would be using this batch file code without usage of text files at all.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "F:\My Pictures\*.1.JPG" /A-D /B /S 2^>nul') do (
for %%J in ("%%~dpnI") do (
if exist "%%~dpnJ%%~xI" (
del "%%I"
) else (
ren "%%I" "%%~nJ%%~xI"
)
)
)
endlocal
The FOR loop starts with %ComSpec% /C one more cmd.exe command process in background to execute the command line:
dir "F:\My Pictures\*.1.JPG" /A-D /B /S 2>nul
DIR searches with the specified options for
files because of option /A-D (attribute not directory)
matching case-insensitive the pattern *.1.JPG
in directory F:\My Pictures and all its subdirectories because of option /S
and outputs in bare format because of option /B just
file name with file extension and with full path because of option /S.
DIR would output an error message in case of no file can be found in entire directory tree matching these criteria. This error message is suppressed by redirecting it to device NUL.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR captures everything output to handle STDOUT of started command process and processes the captured text line by line after started cmd.exe finished.
FOR ignores empty lines which do not occur here at all. FOR would also ignore lines starting with ; because of being the default for end of line option. As DIR outputs the file names with full path, it is not possible that a line starts with ;. But eol=| is nevertheless used to define the vertical bar as end of line which no folder/file name can contain ever.
FOR splits up by default each line into substrings (tokens) using normal space and horizontal tab character as delimiters. This behavior is not wanted here as file path could contain a space character. For that reason delims= is used to define an empty list of delimiters which disables the line splitting behavior.
The inner FOR is used to get assigned to loop variable J just the string left to .1.JPG.
The IF condition checks if there is already a file *.JPG for current file *.1.JPG in same directory as current file in which case the file *.1.JPG is deleted or otherwise the renamed to *.JPG if this file deletion or file rename operation is permitted at all depending on read-only attribute, file permissions of current account and current file sharing access permissions.
But let us assume the image file names can be any file name matching *.jpg and there can be not only *.1.jpg, but also *.2.jpg to *.99.jpg image files, i.e. any number after a dot before file extension .jpg. In this case DIR is not enough to get the list of file names with file extension and full path. It is additionally necessary to use FINDSTR with a regular expression to filter the list of file names.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "F:\My Pictures\*.*.jpg" /A-D /B /S 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R "\.[0123456789][0123456789]*\.jpg$"') do (
for %%J in ("%%~dpnI") do (
if exist "%%~dpnJ%%~xI" (
del "%%I"
) else (
ren "%%I" "%%~nJ%%~xI"
)
)
)
endlocal
First FINDSTR outputs just lines read from STDIN which
matches case-sensitive because of option /I
the regular expression \.[0123456789][0123456789]*\.jpg$
as explicitly declared with option /R.
The regular expression matches a string consisting of a dot, one or more digits, one more dot and the string jpg found at end of line. So a file name like Hello.World.jpg output by DIR is not matched by FINDSTR and therefore not output by FINDSTR and so not processed by FOR.
But a file name like Hello.World.393.jpg is processed and either deleted or renamed to Hello.World.jpg depending on existence of Hello.World.jpg in same directory.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
dir /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
ren /? or rename /?
set /?
setlocal /?
See also Where does GOTO :EOF return to?
Like the title suggests I want to batch rename files but keep the part of rar file.
Example:
File-01.part1.rar
File-01.part2.rar
Output:
Newfile__01.part1.rar
Newfile__01.part2.rar
Below is the rename part of the code.
SETLOCAL EnableDelayedExpansion
REM Set your Year, Month, Day variable values here.
REM They will be used for file renaming.
CD "D:\test\rename"
FOR /F "usebackq tokens=* delims=" %%A IN (`DIR "*.rar" /B /A:-D`) DO (
REM Extract the last 2 chars of the file name.
SET FileName=%%~nA
SET First4=!FileName:~0,5!
SET Last2=!FileName:~-2!
REM Rename the file, inserting the new data.
RENAME "%%A" "!First4!__!Last2!%%~xA"
)
ENDLOCAL
Here's an example script based on my assumption of what you wanted to do:
#Echo Off
SetLocal EnableDelayedExpansion
Set "sd=D:\test\rename"
Set "xf=.rar"
If Not Exist "%sd%\*%xf%" Exit /B
CD /D "%sd%" 2>Nul || Exit /B
Set "ds="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A%%B%%C"
For %%A In ("*%xf%") Do (Set "fn=%%~nA"
For %%B In ("!fn!") Do (Set "fx=%%~xB"
If /I Not "!fx:.part=!"=="%%~xB" Set "fn=%%~nB")
Ren "%%A" "!fn:~,4!_%ds%_!fn:~-2!!fx!%xf%$")
Ren "*%xf%$" "*%xf%"
I have slotted the year, month, day variable, %ds% in between the two underscores, meaning:
File-01.part1.rar would be renamed to File_20180523_01.part1.rar
Testing18.part2.rar would be renamed to Test_20180523_18.part2.rar
Archive02.rar would be renamed to Arch_20180523_02.rar.
Try this batch file after modification of folder path in third line:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Folder=D:\test\rename"
for /F "eol=| delims=" %%A in ('dir "%Folder%\File-01.part*.rar" /A-D-H /B /ON 2^>nul') do (
for /F "eol=| delims=" %%B in ("%%~nA") do ECHO ren "%Folder%\%%A" "Newfile__01%%~xB%%~xA"
)
endlocal
pause
Note: The command ECHO is used here on line 6 to just demonstrate what would be the rename command line. Run the batch file as is for verification. When everything looks okay, remove ECHO and run the batch file once again to really rename the files.
This batch file runs FOR which runs the command DIR with using a separate command process started in background with cmd.exe /C to output
just the names in bare format because of /B
of only non hidden files because of /A-D-H (attribute not directory and not hidden)
sorted by name because of /ON
matching in specified folder the wildcard pattern File-01.part*.rar.
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background. 2>nul redirects the error message output on no file matching the criteria from handle STDERR to device NUL to suppress it.
FOR captures the output of started command process and processes it line by line with ignoring empty lines.
The default end of line character ; is modified with eol=| from semicolon to vertical bar. FOR ignores by default lines starting with a semicolon. A file name can start with a semicolon. But a file name can't contain a vertical bar.
FOR splits up by default a line on spaces/tabs and assigns just first part to specified loop variable A. The line splitting behavior is disabled by delims= which specifies an empty list of delimiters resulting in getting the file name with file extension but without file path as output by DIR assigned to the loop variable.
The inner FOR loop processes as string just the file name without file extension. This is the string from first character up to character before last dot, i.e. File-01.part1 and File-01.part2 and assigns this part to loop variable B. For the inner FOR the file extension is again everything after last dot which is .part*. The real file extension .rar of the file is unknown for the inner FOR loop processing just a file name string.
The command REN renames the current file referenced full path to new file name with keeping .part* referenced with %%~B from inner FOR loop and original file extension .rar from outer FOR loop.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
pause /?
ren /?
setlocal /?
I have a batch script used to extract PDF information and rename the PDF then.
The script is working fine for 1 PDF file but I need to use it directly in a folder with a lot of PDF files.
So how to do it?
The script needs to run for each PDF file, one by one until the end.
Once the PDF file is renamed next the file is moved in another folder, so the PDF file remaining in the folder need the same thing. And when the folder is empty the script exits.
#echo off
setlocal enabledelayedexpansion
set PDF="Renommer_Manuellement.pdf"
set text="Renommer_Manuellement.txt"
set DSUBDIX=%USERPROFILE%\Google Drive\CLASSEURS
ren *.pdf %PDF%
pdftotext -raw %PDF%
for /f "delims=- tokens=2" %%a in ('find "Number=" %text%') do set numeroa=%%a
for /f "delims== tokens=2" %%a in ('find "NAME=" %text%') do set nature=%%a
ren "%PDF%" "OCC-%numeroa:~0,5%#!nature!.pdf"
move "%PDF%" "OCC-%numeroa:~0,5%#!nature!.pdf" "XYZ FOLDER"
exit
Perhaps this commented batch file code works for moving all PDF files in current directory to subdirectory XYZ FOLDER with new file name determined from contents of each PDF file.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Don't know what this environment variable is for. It is not used by this code.
set "DSUBDIX=%USERPROFILE%\Google Drive\CLASSEURS"
md "XYZ FOLDER" 2>nul
for /F "delims=" %%I in ('dir *.pdf /A-D /B 2^>nul') do call :RenamePDF "%%~fI"
rem Restore initial command environment and exit batch file processing.
endlocal
goto :EOF
:RenamePDF
set "FilePDF=%~1"
set "FileTXT=%~dpn1.txt"
pdftotext.exe -raw "%FilePDF%"
for /F "delims=- tokens=2" %%J in ('%SystemRoot%\System32\find.exe "Number=" "%FileTXT%"') do set "numeroa=%%J"
for /F "delims== tokens=2" %%J in ('%SystemRoot%\System32\find.exe "NAME=" "%FileTXT%"') do set "nature=%%J"
rem The text version of the PDF file is no longer needed.
del "%FileTXT%"
rem Move the PDF file to XYZ FOLDER and rename the file while moving it.
move "%FilePDF%" "XYZ FOLDER\OCC-%numeroa:~0,5%#%nature%.pdf"
rem The file movement could fail in case of a PDF file with new
rem name exists already in target folder. In this case rename
rem the PDF file in its current folder.
if errorlevel 1 ren "%FilePDF%" "OCC-%numeroa:~0,5%#%nature%.pdf"
rem Exit the subroutine RenamePDF and continue with FOR loop in main code.
goto :EOF
It is unclear for me why each *.pdf file should be moved into a subdirectory with new file name.
This is not really necessary in my point of view. The command REN could be enough.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
dir /?
echo /?
endlocal /?
find /?
for /?
goto /?
md /?
move /?
rem /?
ren /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
See also Where does GOTO :EOF return to?
My photo import tool (Picasa) does a great job at importing photos and videos from my phone and camera. What I like is that it creates a subfolder under the Pictures directory based on the Photo Taken Date of each photo/video. So you end up with this structure:
C:\Pictures\2017-02-01\DSC_0001.jpg
C:\Pictures\2017-02-01\DSC_0002.jpg
C:\Pictures\2017-02-01\DSC_0003.mp4 <--- problem
The only problem is that it puts videos in this same structure under Pictures.
As such, I'd like to right a batch script to find and move all video files (.mp4, .avi, .mov) from the C:\Pictures directory to the C:\Videos directory, but also with the date subfolder....
i.e.
Move C:\Pictures\2017-02-01\DSC_0003.mp4 to C:\Videos\2017-02-01\DSC_0003.mp4
Note that the date subfolder may or may not exist under C:\Videos.
Also since these are large video files, and there are a lot of them, I'd prefer a process that actually does a move and not a copy then delete, for the sake of speed and disk space utilization as I am almost out of space (after re-organizing these files, I will be archiving off to a NAS).
Also prefer using RoboCopy, xcopy, or xxcopy as I have them and use them today on my machine. If massively easier using PowerShell scripting, I can learn that if it is easy to do.
Final Solution
I used Mofi's answer, but enhanced it just a bit to add a function to calculate the directory string length
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define folder with the pictures which is never deleted.
set "PicturesFolder=D:\Users\Chad\PicturesTest"
rem get string length of source directory to later use in a substring type function
call :strlen PicturesFolderDirectoryLength PicturesFolder
echo PicturesFolderDirectoryLength = %PicturesFolderDirectoryLength%
rem Change the current directory to directory with the pictures.
cd /D "%PicturesFolder%"
rem Search recursive in this directory for video files with
rem file extension AVI, MOV, MP4 or MPG and move those files.
for /F "delims=" %%I in ('dir /A-D /B /S *.avi *.mov *.mp4 *.mpg 2^>nul') do call :MoveVideo "%%I"
rem Discard all environment variables defined in this batch code
rem and restore initial current directory before exiting batch file.
endlocal
goto :EOF
rem MoveVideo is a subroutine called with name of current
rem video file name with full path by the FOR loop above.
rem It first defines target path for video file depending on source path
rem by removing the backslash at end and concatenating C:\Videos with the
rem source path omitting the first 11 characters which is C:\Pictures.
rem Then the target directory structure is created with redirecting the
rem error message output by command MD to handle STDERR in case of the
rem target directory already exists to device NUL to suppress it.
rem Next the video file is moved from source to target folder with silently
rem overwriting an already existing file with same name in target folder
rem because of using option /Y. Remove this option if a video file should
rem be kept in pictures folder and an error message should be displayed in
rem case of a video file with same name already existing in target folder.
rem Last the source folder is removed if it is completely empty which means
rem it does not contain any file or subfolder. All parent folders up to the
rem pictures folder are also removed if each parent folder is also empty
rem after deletion of an empty folder.
rem The subroutine is exited with goto :EOF and execution of batch file
rem continues in main FOR loop above with next found video file.
:MoveVideo
set "SourcePath=%~dp1"
set "SourcePath=%SourcePath:~0,-1%"
ECHO SourcePath=%SourcePath%
CALL SET "SourceSubFolder=%%SourcePath:~%PicturesFolderDirectoryLength%%%"
ECHO SourceSubFolder=%SourceSubFolder%
set "TargetPath=D:\Users\Chad\VideosTest%SourceSubFolder%"
echo TargetPath=%TargetPath%
md "%TargetPath%" 2>nul
move /Y "%~1" "%TargetPath%\%~nx1" >nul
:DeleteSourceFolder
rd "%SourcePath%" 2>nul
if errorlevel 1 goto :EOF
for /F "delims=" %%D in ("%SourcePath%") do set "SourcePath=%%~dpD"
set "SourcePath=%SourcePath:~0,-1%"
if /I not "%SourcePath%" == "%PicturesFolder%" goto DeleteSourceFolder
goto :EOF
:strlen <resultVar> <stringVar>
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
Here is a commented batch code for this file moving task with keeping directory structure.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define folder with the pictures which is never deleted.
rem Note: ~11 in third line of subroutine MoveVideo must be
rem replaced by ~length of the folder path defined here.
set "PicturesFolder=C:\Pictures"
rem Change the current directory to directory with the pictures.
cd /D "%PicturesFolder%"
rem Search recursive in this directory for video files with
rem file extension AVI, MOV, MP4 or MPG and move those files.
for /F "delims=" %%I in ('dir /A-D /B /S *.avi *.mov *.mp4 *.mpg 2^>nul') do call :MoveVideo "%%I"
rem Discard all environment variables defined in this batch code
rem and restore initial current directory before exiting batch file.
endlocal
goto :EOF
rem MoveVideo is a subroutine called with name of current
rem video file name with full path by the FOR loop above.
rem It first defines target path for video file depending on source path
rem by removing the backslash at end and concatenating C:\Videos with the
rem source path omitting the first 11 characters which is C:\Pictures.
rem Then the target directory structure is created with redirecting the
rem error message output by command MD to handle STDERR in case of the
rem target directory already exists to device NUL to suppress it.
rem Next the video file is moved from source to target folder with silently
rem overwriting an already existing file with same name in target folder
rem because of using option /Y. Remove this option if a video file should
rem be kept in pictures folder and an error message should be displayed in
rem case of a video file with same name already existing in target folder.
rem Last the source folder is removed if it is completely empty which means
rem it does not contain any file or subfolder. All parent folders up to the
rem pictures folder are also removed if each parent folder is also empty
rem after deletion of an empty folder.
rem The subroutine is exited with goto :EOF and execution of batch file
rem continues in main FOR loop above with next found video file.
:MoveVideo
set "SourcePath=%~dp1"
set "SourcePath=%SourcePath:~0,-1%"
set "TargetPath=C:\Videos%SourcePath:~11%"
md "%TargetPath%" 2>nul
move /Y "%~1" "%TargetPath%\%~nx1" >nul
:DeleteSourceFolder
rd "%SourcePath%" 2>nul
if errorlevel 1 goto :EOF
for /F "delims=" %%D in ("%SourcePath%") do set "SourcePath=%%~dpD"
set "SourcePath=%SourcePath:~0,-1%"
if /I not "%SourcePath%" == "%PicturesFolder%" goto DeleteSourceFolder
goto :EOF
This batch file also removes all folders in C:\Pictures which become empty after moving the video files. But it does not remove folders which were already empty on starting the batch file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
move /?
rd /?
rem /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of >nul and 2>nul. In the main FOR loop the redirection operator > is escaped with caret character ^ to be interpreted as literal character on parsing FOR command line and later as redirection operator on execution of DIR command line by FOR.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
XCOPY /T "%sourcedir%" "%destdir%"
FOR %%x IN (mp4 mov) DO (
FOR /f "tokens=1*delims=>" %%a IN (
'XCOPY /Y /s /d /F /L "%sourcedir%\*.%%x" "%destdir%"'
) DO IF "%%b" neq "" (
SET "topart=%%b"
SET "frompart=%%a"
ECHO(MOVE /y "!frompart:~0,-2!" "!topart:~1!"
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The first xcopy creates the required subtrees, the second uses the /L option to list rather than copy the files
The loop on %%x assigns %%x to the required extensions. The output from the inner xcopy will be of the form fullsourcefilename -> fulldestinationfilename so it needs to be parsed using > as a delimiter, from-filename to %%a, to-filename to %%b. If %%b is not set, then this is the final line of xcopy's report (n files copied) which needs to be ignored. The to and from filenames need to be trimmed of unwanted, but fortunately constant character strings.
What is interesting is that there appears to be no way using xcopy to suppress prompting in the case where the destination filename already exists.