batch file loop not showing most recent directory - windows

I am trying to loop through one directory and pic out the sub directory, which may be buried within other directories, and pull out the directory that has been most recently created. I have a loop here which will go into the root directory, which is set, and look at only directories, incorporate all sub directories, sort them by creation date, and have the loop set the one at the end of the list be the most recent which is then echoed out. However, for some reason the sorting does not happen correctly. It keeps pulling out a directory that is not the most recent. I cannot seem to pinpoint the issue. What could be causing this? Am I using the sorting correctly? Does it not compare sub directories to other directories on other levels? Any help would be greatly appreciated.
#ECHO OFF
SET dir=C:\Users\Darren\Google Drive\
echo %dir%
FOR /F "delims=" %%i IN ('dir "%dir%" /b /s /ad-h /t:c /od') DO (
echo %%i
SET a=%%i)
SET sub=%a%
echo Most recent subfolder: %sub%

#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43579378.txt"
SET "filename2=%sourcedir%\q43579378_2.txt"
(
FOR /f "delims=" %%a IN (
'dir /b /s /ad "%sourcedir%\*" '
) DO ECHO %%~a
)>"%filename1%"
IF EXIST "%filename2%" (
FOR /f "delims=" %%a IN ('findstr /L /V /i /x /g:"%filename2%" "%filename1%"') DO (
FOR /f %%c IN ('dir /b /a-d "%%a\*" 2^>nul^|find /c /v "" ') DO (
IF %%c gtr 20 ECHO(send email directory %%a has %%c files
)
)
)
MOVE "%filename1%" "%filename2%" >nul
GOTO :EOF
I used a test directory as sourcedir. Naturally, it doesn't have to be the same as the directory you're scanning, and the filenames can be whatever you like.
The first for builds a list of the absolute dirctorynames currently existing into filename1 (the "spare" pair of parentheses enclosing the for allows the redirection)
If filename2 exists (which will be for every run after the first), then find the difference between filename1 and filename2, using /L literal, /v doesn't match /i case-insensitive /x exact match /g: strings in this file and assign each new directoryname to %%a.
Then scan the directory of "%%a", files only, suppressing file not found messages and counting the number of returned lines (/c count /v lines not matching "" an empty string). Assign the re4sult to %%c, test and choose where to take action.
the carets ^ before the redirectors in the command-to-be-executed of the for ... %%c command escape the redirectors so that they are interpreted as part of the command-to-be-executed, not the for.

The dir /S /O command sorts the content of every sub-directory individually, and there is no way to change that behaviour.
A possible alternative approach is to use the wmic command, which is a bit slow but capable of deriving the date information (creation, last modification, last access) in a standardised, sortable, region- and locale-independent format:
wmic FSDir where Name="D:\\Data" get CreationDate /VALUE
wmic FSDir where (Name="D:\\Data") get CreationDate /VALUE
So below are two scripts that use a for /D /R loop to get all the (sub-)directories, the above wmic command lines, for /F to capture their output and a simple command to do the sorting by age.
The first one creates a temporary file to collect all directories, together with their creation dates, in the following format:
20170424215505.000000+060 D:\Data
For sorting, the sort command is used. Here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~1" & rem // (use first command line argument as the root directory)
set "_PATTERN=*" & rem // (search pattern for directories; `*` matches all)
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (specify a temporary file)
rem // Write list of directory paths preceded by their creation dates to temporary file:
> "%_TMPF%" (
set "ERR=0"
rem // Enumerate all matching directories recursively:
for /D /R "%_ROOT%" %%D in ("%_PATTERN%") do (
rem // Store currently iterated directory path:
set "DIRPATH=%%~D"
rem // Toggle delayed expansion to avoid trouble with the exclamation mark:
setlocal EnableDelayedExpansion
(
rem /* Capture `wmic` output to query creation date of currently iterated
rem directory in locale-dependent and sortable format: */
for /F "tokens=2 delims==" %%L in ('
rem/ Do two attempts, because one approach can handle `^)` and one can handle `,`; ^& ^
rem/ note that `wmic` cannot handle paths containing both of these characters: ^& ^
2^> nul wmic FSDir where Name^="!DIRPATH:\=\\!" get CreationDate /VALUE ^|^| ^
2^> nul wmic FSDir where ^(Name^="!DIRPATH:\=\\!"^) get CreationDate /VALUE
') do (
rem // Do nested loop to avoid Unicode conversion artefacts (`wmic` output):
for /F %%K in ("%%L") do echo(%%K !DIRPATH!
)
) || (
rem // This is only executed in case a path contains both `)` and `,`:
>&2 echo ERROR: Could not handle directory "!DIRPATH!"^^!
set "ERR=1"
)
endlocal
)
)
rem /* Return content of temporary file in sorted manner using `sort` command,
rem remember last item of sorted list; clean up temporary file: */
for /F "tokens=1*" %%C in ('sort "%_TMPF%" ^& del "%_TMPF%"') do set "LASTDIR=%%D"
rem // Return newest directory:
echo "%LASTDIR%"
endlocal
exit /B %ERR%
The second one stores each directory in a variable named of the creation date, in the following format:
$20170424215505.000000+060=D:\Data
For sorting, the set command is used. Here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~1" & rem // (use first command line argument as the root directory)
set "_PATTERN=*" & rem // (search pattern for directories; `*` matches all)
set "ERR=0"
rem // Clean up variables beginning with `$`:
for /F "delims==" %%C in ('2^> nul set "$"') do set "%%C="
rem // Enumerate all matching directories recursively:
for /D /R "%_ROOT%" %%D in ("%_PATTERN%") do (
rem // Store currently iterated directory path:
set "DIRPATH=%%~D"
rem // Toggle delayed expansion to avoid trouble with the exclamation mark:
setlocal EnableDelayedExpansion
(
rem /* Capture `wmic` output to query creation date of currently iterated
rem directory in locale-dependent and sortable format: */
for /F "tokens=2 delims==" %%L in ('
rem/ Do two attempts, because one approach can handle `^)` and one can handle `,`; ^& ^
rem/ note that `wmic` cannot handle paths containing both of these characters: ^& ^
2^> nul wmic FSDir where Name^="!DIRPATH:\=\\!" get CreationDate /VALUE ^|^| ^
2^> nul wmic FSDir where ^(Name^="!DIRPATH:\=\\!"^) get CreationDate /VALUE
') do (
rem // Do nested loop to avoid Unicode conversion artefacts (`wmic` output):
for /F %%K in ("%%L") do (
rem /* Assign currently iterated path to variable named of the
rem respective creation date preceded by `$`: */
endlocal & set "$%%K=%%~D"
)
)
) || (
endlocal
rem // This is only executed in case a path contains both `)` and `,`:
>&2 echo ERROR: Could not handle directory "%%~D"!
set "ERR=1"
)
)
rem /* Return all variables beginning with `$` in sorted manner using `set` command,
rem remember last item of sorted list: */
for /F "tokens=1* delims==" %%C in ('2^> nul set "$"') do set "LASTDIR=%%D"
rem // Return newest directory:
echo "%LASTDIR%"
endlocal
exit /B %ERR%

Related

Batch: Create folders from filename (substring)

i have loads of files which i want to organize differently. The batch script should create folders with the substring on the left side of the date in the filename.
Files are now named like this:
This_is_my_file_21.01.29_22-00_abc_115.avi
This_is_my_file_20.09.29_21-10_abc_15.avi
This_is_another_file_21.01.29_22-00_abc_55.avi
Pattern:
<Name with unknown number of underscores>_<YY.MM.DD>_<hh-mm>_<string with unknown length>_<number n from 1-999>.avi
Folders should be named like this:
This_is_my_file <- two files will go into this directory
This_is_another_file <- only one file.
The Problem is, how do I get the correct substring for my folder name?
This is what I have so far:
#echo off
setlocal
set "basename=."
for /F "tokens=1* delims=." %%a in ('dir *.avi /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename!"
md "!basename:~0,-23!"
)
move "!filename!.%%b" "!basename:~0,-23!"
for /F "delims=" %%c in ("!basename!") do (
endlocal
set "basename=%%c
)
)
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
FOR /f "delims=" %%b IN ('dir /b /a-d "%sourcedir%\*.avi" ' ) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
CALL :countus "%%b"
IF DEFINED subdir (
MD "!subdir!" 2>NUL
ECHO MOVE "%sourcedir%\%%b" "%sourcedir%\!subdir!\"
) ELSE (
ECHO Failed pattern check %%b
)
ENDLOCAL
)
GOTO :EOF
:: count number of underscores before pattern YY.MM.DD_hh-mm
:countus
SET /a ucount=0
:countusloop
SET /a ucount+=1
SET /a scount=ucount+1
FOR /f "tokens=%ucount%,%scount%delims=_" %%q IN ("%~1") DO SET "str1=%%q"&SET "str2=%%r"
IF NOT DEFINED str2 SET "subdir="&GOTO :EOF
:: is %str1%.%str2:-=.%. of form np.np.np.np.np where np is a number-pair?
SET "candidate=%str1%.%str2:-=.%."
FOR /L %%c IN (10,1,99) DO IF DEFINED candidate SET "candidate=!candidate:%%c.=!"&IF NOT DEFINED candidate GOTO success
FOR /L %%c IN (0,1,9) DO IF DEFINED candidate SET "candidate=!candidate:0%%c.=!"&IF NOT DEFINED candidate GOTO success
GOTO countusloop
:success
SET "subdir=%~1"
FOR /f "delims=:" %%e IN ("!subdir:_%str1%_%str2%=:!") DO SET "subdir=%%e"
GOTO :eof
The "move" command is merely echoed for verification. Remove the echo from echo move to actually move the files.
This possible solution uses the fact that your filenames have a known number of underscores if you work backwards. All I do is replace those underscores with backslashes, which obviously cannot already be contained in the filename. I can then use the relative paths to step up the filename, as if it were a directory tree, until all I have left is the part ahead of the date sequence, which I then replace the backslashes with underscores again. I use the result of that with robocopy, which has a move option, and will create the destination directory automatically, if it does not already exist. At the outset, I perform the directory search, in the same directory as the batch-file, using where.exe, (you can change that, on line three, from "%~dp0." to ".", if you want to use the current directory instead, or "any other path" as necessary). where.exe not only treats the ? wildcard as exactly one character, (unlike the dir command which is zero or one), but also ignores 8.3 naming. It therefore treats the .avi extension exactly as written, (and not 'beginning with' .avi, which dir, or a standard for loop, would).
Anyhow, feel free to give it a try:
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "}=" & For /F Delims^= %%G In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe
"%~dp0.":"?*_??.??.??_??-??_?*.avi" 2^> NUL') Do (Set "}=%%~nG"
SetLocal EnableDelayedExpansion & For %%H In ("\!}:_=\!") Do (
EndLocal & For %%I In ("%%~pH..\..") Do (Set "}=%%~pI"
SetLocal EnableDelayedExpansion & Set "}=!}:~1,-1!"
For %%J In ("!}:\=_!") Do (EndLocal & %__AppDir__%robocopy.exe ^
"%%~dpG." "%%~dpG%%~J" "%%~nxG" /Mov 1> NUL))))
If you want even further robustness, and do not wish to use a more suitable scripting technology, the following, extremely complex looking, version, is the same code, except that it uses findstr to validate the date and time sequence. It filters those avi files containing the following pattern, _yy.MM.dd_hh-mm_ in the avi filenames, using all dates from the beginning of 1970 up until the end of 2021:
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "}=" & For /F Delims^= %%G In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe
"%~dp0.":"?*_??.??.??_??-??_?*.avi" 2^> NUL ^| %__AppDir__%findstr.exe
/RC:"_[789][0123456789].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[789][0123456789].1[012].3[01]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_[01][0123456789].1[012].3[01]_2[0123]-[012345][0123456789]_"
/C:"_2[01].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
/C:"_2[01].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
/C:"_2[01].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
/C:"_2[01].1[012].3[01]_2[0123]-[012345][0123456789]_"') Do (Set "}=%%~nG"
SetLocal EnableDelayedExpansion & For %%H In ("\!}:_=\!") Do (
EndLocal & For %%I In ("%%~pH..\..") Do (Set "}=%%~pI"
SetLocal EnableDelayedExpansion & Set "}=!}:~1,-1!"
For %%J In ("!}:\=_!") Do (EndLocal & %__AppDir__%robocopy.exe ^
"%%~dpG." "%%~dpG%%~J" "%%~nxG" /Mov 1> NUL))))

Move file to trimmed filename location with full name in Batch

I'm trying to move several similar files into folders based on the filename.
The code below works fine but does not work if the base name is more than 5 characters, then it says the directory already exists, and moves the files to the shorter named folder.
The idea is to make folders based on a text file, and move it together with pictures which start with the same name (up to an "_" to that same folder, while the filenames remain intact. The picture names are longer though with varying lengths.
eg:
SB12.txt
SB123.txt
SB1234.txt
SB12345.txt
SB123_V_05062020.jpg
SB123_VT_05062020.jpg
SB12345_V_05062020.jpg
SB12345_VT_05062020.jpg
I tried adding delims=_ to the loop parameters but does not work like this :
for /f "tokens=* delims=_ " %%f in ('dir /b /on "%dir%\*%ext%"') do (
I already "solved" the longer name problem by changing the wildcard to >.* like the line below, but then the pictures don't get moved:
move "%dir%\!thefile!>.*" "%dir%\%yyyymmdd%\!thefile!\"
full code:
#echo off
setlocal
REM store current directory. Using separate variable makes it easier to change behavior too.
set dir=%cd%
REM make date fitting for folder needs
for /f "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k\%%j\%%i
REM call subroutine for each supported extension.
call :dotxt .txt
REM call :dojpg .jpg
REM Main program done.
echo Press a key to close.
pause
exit /b
:dotxt
set ext=%1
REM loop through all files with the given extension.
for /f "tokens=*" %%f in ('dir /b /on "%dir%\*%ext%"') do (
REM trim the extension and use the base name as directory name.
setlocal EnableDelayedExpansion
set thefile=%%~nf
echo !thefile!
md "%dir%\%yyyymmdd%\!thefile!"
REM move all files that start with the same base name.
move "%dir%\!thefile!*.*" "%dir%\%yyyymmdd%\!thefile!\"
)
%SystemRoot%\explorer.exe %dir%\%yyyymmdd%
REM exit subroutine
exit /b
I think I might need an additional loop or another "set" option but can't get it figured out on my own.
A somewhat easier way, assuming you've already defined Dir and yyyymmdd
#Echo off
Set "Dir=Path of Root\"
Set "yyyymmdd=Define Date Substring"
::: { Set environment state for Macro Definitions
Setlocal DisableDelayedExpansion
(Set LF=^
%= Above Empty lines Required =%)
Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
::: { Macro for relocation of files to folders using Substring of Filename
::: - Usage:
::: %MOVEIF%{Search String*.Extension}{Destination Directory}
%= Capture Arguments =%
SET MOVEIF=For %%n in (1 2) Do If %%n==2 (%\n%
%= Split arguments using braces as delims =%
For /F "Tokens=1,2 Delims={}" %%G in ("!ITEM!") Do (%\n%
%= Use Arg 1 as search pattern for files in current and sub directories =%
For /F "Delims=" %%F in ('dir "%%~G" /b /s') do (%\n%
%= Split the name of file from _ for use as folder name =%
For /F "Tokens=1 Delims=_" %%N in ("%%~nF") Do (%\n%
%= Test / Create Subfolder in target Subdirectory using Arg 2 =%
IF Not exist "%%~H\%%~N" MD "%%~H\%%~N"%\n%
%= Execute move =%
Move "%%~F" "%%~H\%%~N"%\n%
)%\n%
)%\n%
)%\n%
) Else Set ITEM=
::: }
::: - enable macro, execute with args.
Setlocal EnableDelayedExpansion
CD "%Dir%" && For %%x in (jpg txt) Do For %%p in (SB) Do (%MOVEIF%{%%~p*.%%~x}{%Dir%%yyyymmdd%}) 2> Nul
Pause
Exit /B 0
Note: It isn't necessary to use a macro to achieve this, however it makes it very easy to use the code for other search strings or directories, as there is no need to edit the macro, you just call it with different parameters.
This is not a particularly complex task – here is a possible script (see all the rem comments):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~dp0." & rem // (path to directory containing target files)
set "_MASK=SB*.txt" & rem /* (file name pattern to find the master files,
rem which sub-directory names are derived from) */
set "_SUFF=_*.jpg" & rem /* (suffix pattern to be appended to the base
rem name of a master file to find related slave
rem files, which are to be moved in addition) */
rem // Change into the root directory:
pushd "%_ROOT%" && (
rem // Loop through the found master files:
for /F "delims= eol=|" %%F in ('dir /B /A:-D-H-S "%_MASK%"') do (
rem // Create sub-directory with base name of current master file:
md "%%~nF" 2> nul
rem // Move the master file into the sub-directory:
move "%%F" "%%~nF\" > nul
rem // Move related slave files into the sub-directory:
if exist "%%~nF%_SUFF%" move "%%~nF%_SUFF%" "%%~nF\" > nul
)
rem // Return from the root directory:
popd
)
endlocal
exit /B
With some help I was able to get my script working, it just doesnt like spaces bu that's fine for me:
#echo off
setlocal
REM store current directory. Using separate variable makes it
REM easier to change behaviour too.
set dir=%cd%
REM make date fitting for folder needs
for /f "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k\%%j\%%i
REM call subroutine for each supported extension.
call :do .txt
call :do .jpg
REM Main program done.
echo Press a key to close.
pause
exit /b
:do
set ext=%1
REM loop through all files with the given extension.
for /f %%f in ('dir /b /on "%dir%\*%ext%"') do (
echo %%f
for /f "tokens=1 delims=_." %%m in ("%%f") do (
echo %%m
REM Make the folder
if not exist "%dir%\%yyyymmdd%\%%m" mkdir "%dir%\%yyyymmdd%\%%m"
echo %dir%\%%m\%%f
move ".\%%f" "%dir%\%yyyymmdd%\%%m"
)
)
REM %SystemRoot%\explorer.exe %dir%\%yyyymmdd%
REM exit subroutine
exit /b
Thanks for the efforts

Get current position number of an item compared to the total items

I have a batch script that backup some data of each local users to a secondary hard drive, IF they aren't on an exclusion list and IF they have a nominative folder on the secondary drive.
For each user being processed (that meets above criterias), I'd like to display their current position within the remaining users.
For example: "Processing user 1 / 10 : Thomas"
Here is my code:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set BackupDest=D:\backup
for /f "tokens=*" %%I in ('dir /a:d-h /b "%HomeDrive%\users\*" ^| findstr /b /e /i /l /v /g:"%~dp0exclude_users.txt"') do if exist "%BackupDest%\%%~nI\" (
echo -----------------------------------------
echo Processing user : %%~nXI
echo -----------------------------------------
echo
xcopy "%%~nXI\Desktop" "%BackupDest%\%%~nXI\Desktop\" /e /i /y
xcopy "%%~nXI\Documents" "%BackupDest%\%%~nXI\Documents\" /e /i /y
)
pause
exit
I'm not sure how I can do this.
Well, you will have to count the number of matching items in advance. I suggest to use a dual-loop approach &dash; the first loop gathers the matching items, counts them and writes them into a temporary file, and the second loop reads the temporary file and processes the listed items &dash; like implemented in the following script (see all the explanatory rem remarks):
#echo off
rem // You do not need delayed expansion, so keep it disabled to avoid trouble with `!`:
setlocal EnableExtensions DisableDelayedExpansion
rem /* Use the safe quoted `set` syntax (this requires the command extensions to be enabled, but
rem I enabled them above, though this was the default anyway):
set "BackupDest=D:\backup"
rem // Specify full path to a temporary file:
set "TempFile=%TEMP%\%~n0_%RANDOM%.tmp"
rem // Gather carriage-return character (needed for later display outpout):
for /F %%K in ('copy /Z "%~f0" nul') do set "CR=%%K"
rem /* Change to the source directory, because `dir /B` only returns pure file/directory names;
rem this way it does not matter where and how you execute this script: */
pushd "%HomeDrive%\Users" && (
rem // Initialise counters:
set /A "Count=0, Index=0"
rem // Write list of matching user directories into temporary file:
> "%TempFile%" (
rem // In this loop just count matching items and list them in the temporary file:
for /F "tokens=*" %%I in ('
rem/ // The `findstr` options `/B` and `/E` can be expressed as `/X`: ^& ^
dir /A:D-H /B "*" ^| findstr /X /I /L /V /G:"%~dp0exclude_users.txt"
') do (
rem // `%%I` already contains a pure name, so there is no need for a `~nx` modifier:
if exist "%BackupDest%\%%I\" (
rem // The current user directory is to be processed, hence output it:
echo(%%I
rem // Increment counter, which will eventually contain the total number:
set /A "Count+=0"
)
)
)
rem // In this loop do the actual processing of the items listed in the temporary file:
for /F "usebackq delims=" %%J in ("%TempFile%") do (
rem // Build display output without trailing line-break (just as an alternative option):
set /A "Index+=1" & set "Name=%%J" & setlocal EnableDelayedExpansion
< nul set /P ="Processing user !Index!/!Count!: !Name! !CR!" & endlocal
rem // Append `> nul` to avoid the output of `xcopy` to interfere with the above:
xcopy /E /I /Y "%%J\Desktop" "%BackupDest%\%%J\Desktop\" > nul
xcopy /E /I /Y "%%J\Documents" "%BackupDest%\%%J\Documents\" > nul
)
rem // Return from source directory:
popd & echo/
)
pause
rem // Clean up temporary file:
del "%TempFile%"
endlocal
rem /* `exit` terminates both the script and the hosting `cmd` instance, `exit /B` prevents the
rem the latter from being quit: */
exit /B

CMD line BATCH file - locating most recent file "LIKE" and renaming it

I got something that has been given me some problems for a little while. I have a list of reports that are .csv files. The way they are organized is:
Call Details Report_1448937644342.csv
Call Details Report_1449662976507.csv
Call Details Report_1450293169999.csv
Initial Call Pricing By Archive Report_1448937621469.csv
Initial Call Pricing By Archive Report_1449662916869.csv
Initial Call Pricing By Archive Report_1450293146194.csv
Location Detail Report_1448937658179.csv
Location Detail Report_1449662949955.csv
Location Detail Report_1450293201330.csv
Location Summary Report_1448937672801.csv
Location Summary Report_1449662994508.csv
Location Summary Report_1450293231606.csv
StartStop (1).csv
StartStop (2).csv
StartStop (3).csv
StartStop.csv
Sensor (1).csv
Sensor (2).csv
Sensor (3).csv
So what I would need is something that I can copy the most recent of each report to a different directory while renaming it without the spaces or numbers (CallDetailsReport, IntialCallPricingByArchiveReport, etc.). So if I would run the batch file now it would take that directory of files, find Most recent of each report, copy and rename it to another directory.
I have tried to use the FOR command, but have had very little luck, the biggest problem I have is the number after the _ varies greatly, but it is always greater. I also thought that maybe I could narrow it down by the most recent files, but the endings always being different is kind of messing me up. I am hoping you guys can help.
I got this so far that gives me a list, but does not narrow it down to the most recent.
FOR %%G IN (Report1*.csv ) do #echo %%G
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "tokens=1*delims= " %%a IN (
'dir /b /a-d /o-d "%sourcedir%\*.csv" '
) DO (
IF "%%b" neq "" IF NOT EXIST "%destdir%\%%a" COPY "%sourcedir%\%%a %%b" "%destdir%\%%a" >nul
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
Read the source directory in basic form without directorynames and in reverse-date order so the latest files matching the mask appear first. Split the filename into two using the space as a separator. If the second part is not empty (ie the space exists) test for the presence of a file thefirstpart in the destination directory and perform the copy if it doesn't exist - consequently it will be copied from the first (latest) file found and not be overwritten.
Adjustments to suit your actual requirement would be in your court.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "tokens=1*delims=_" %%a IN (
'dir /b /a-d /o-d "%sourcedir%\*.csv" '
) DO (
IF "%%b" equ "" (
FOR /f "tokens=1*delims= " %%j IN ("%%a") DO (
IF "%%k" neq "" IF NOT EXIST "%destdir%\%%j" COPY "%sourcedir%\%%a" "%destdir%\%%j"
)
) ELSE (
IF NOT EXIST "%destdir%\%%a" COPY "%sourcedir%\%%a_%%b" "%destdir%\%%a"
)
)
GOTO :EOF
Revision to suit realistic filenames.
Try this:
#echo off
set max=0
for /f "tokens=2 delims=_." %%n in ('dir /b Report1*.csv') do (
if %%n GTR !max! set max=%%n
)
copy "Report1 Number_%max%.csv" otherdir\Report1.csv
Original Answer (based on the original question)
Assuming that the greatest numbers denote the most recent items, the following batch script does what you are looking for:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Set up source and destination directories here:
set "SOURCE=D:\source"
set "DESTIN=D:\destin"
for /F "tokens=1,2,3,4 delims=._ " %%I in ('
2^> nul dir /B /A:-D "%SOURCE%\*.csv" ^| ^
findstr /I /R /C:"^Report[0-9][0-9]* Number_[0-9][0-9]*.csv$"
') do (
set "REPORT=%%I"
set "REPORT=0000000!REPORT:*Report=!"
set "NUMBER=0000000%%K"
set "ITEM-!REPORT:~-8!=%%I.%%L"
set "ITEMS-!REPORT:~-8!-!NUMBER:~-8!=%%I %%J_%%K.%%L"
)
for /F "tokens=2,3 delims=-=" %%I in ('
2^> nul set ITEM-
') do (
for /F "tokens=2 delims==" %%X in ('
2^> nul set ITEMS-%%I-
') do (
set "RECENT=%%X"
)
> nul copy /Y /B "%SOURCE%\!RECENT!" "%DESTIN%\%%J"
)
endlocal
exit /B
Basically this approach builds up array-like variables ITEM- and ITEMS- that hold the numbers in the file names padded with leading zeros to consist of 8 digits, then use set to sort the items alphabetically and retrieve the most recent item. Because of the padding, alphabetic sorting results in the same order as alphanumeric sorting.
Both of these variables are set up in the first for /F loop, which enumerates all the applicable items using dir /B /A:-D "*.csv" | findstr /I /R /C:"^Report[0-9][0-9]* Number_[0-9][0-9]*.csv$". findstr is used to filter the files, because dir cannot so that in the same grade of detail. The variable names contain the zero-padded numbers for proper sorting. The variable values are the original file names (ITEMS-) and the new file names (ITEM-).
The second for /F loop parses the output of set ITEM-, which walks through all the first numbers after the word Report. This loop nests another one, iterating through the output of set ITEMS-, which holds both numbers in the file names. The inner loop stores the current item in variable RECENT and overwrites its value each time. Due to the sort order, the greatest number and therefore the most recent item is stored in RECENT. The outer loop is then actually copying the relative file.
Relying on the sample files you provided, the two arrays will hold the following data:
ITEM- (sorted):
ITEM-00000001=Report1.csv
ITEM-00000002=Report2.csv
ITEM-00000003=Report3.csv
ITEMS- (sorted):
ITEMS-00000001-00000123=Report1 Number_123.csv
ITEMS-00000001-00000126=Report1 Number_126.csv
ITEMS-00000001-00000133=Report1 Number_133.csv
ITEMS-00000002-00000123=Report2 Number_123.csv
ITEMS-00000002-00000126=Report2 Number_126.csv
ITEMS-00000002-00000133=Report2 Number_133.csv
ITEMS-00000003-00000123=Report3 Number_123.csv
ITEMS-00000003-00000126=Report3 Number_126.csv
ITEMS-00000003-00000133=Report3 Number_133.csv
Updated Answer (based on the revised question)
After you changed your original requirements intensively in the most recent revision of your question, I have reworked the script extensively and came up with the following two scripts, each handling a certain name pattern of your *.csv report files. Both scripts rely on a temporary file which is used for appropriate sorting using the sort command, to get the correct items with the greatest numbers:
This script handles all your report files that have a report name, an underscore _ and an index number in their file names.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Set up source and destination directories here:
set "SOURCE=D:\source"
set "DESTIN=D:\destin"
> "%~dpn0.tmp" (
for /F "tokens=1,2 delims=_" %%I in ('
2^> nul dir /B /A:-D "%SOURCE%\*.csv" ^| ^
findstr /I /R /C:"^[^_][^_]*_[0-9][0-9]*.csv$"
') do (
set "REPORT=%%I"
set "NUMBER=00000000000000000000000%%~nJ"
setlocal EnableDelayedExpansion
echo(!REPORT!^|!NUMBER:~-24!^|!REPORT!_%%J
endlocal
)
)
set "FORMER="
< "%~dpn0.tmp" (
for /F "tokens=1,3 delims=|" %%I in ('
sort /R
') do (
set "REPORT=%%I"
set "ORNAME=%%J"
setlocal EnableDelayedExpansion
if /I not "!REPORT!"=="!FORMER!" (
> nul copy /Y /B "%SOURCE%\!ORNAME!" "%DESTIN%\!REPORT: =!%%~xJ"
)
endlocal
set "FORMER=%%I"
)
)
del /Q "%~dpn0.tmp"
endlocal
exit /B
The related temporary file contains the following unsorted data, based on your sample files:
Call Details Report|000000000001448937644342|Call Details Report_1448937644342.csv
Call Details Report|000000000001449662976507|Call Details Report_1449662976507.csv
Call Details Report|000000000001450293169999|Call Details Report_1450293169999.csv
Initial Call Pricing By Archive Report|000000000001448937621469|Initial Call Pricing By Archive Report_1448937621469.csv
Initial Call Pricing By Archive Report|000000000001449662916869|Initial Call Pricing By Archive Report_1449662916869.csv
Initial Call Pricing By Archive Report|000000000001450293146194|Initial Call Pricing By Archive Report_1450293146194.csv
Location Detail Report|000000000001448937658179|Location Detail Report_1448937658179.csv
Location Detail Report|000000000001449662949955|Location Detail Report_1449662949955.csv
Location Detail Report|000000000001450293201330|Location Detail Report_1450293201330.csv
Location Summary Report|000000000001448937672801|Location Summary Report_1448937672801.csv
Location Summary Report|000000000001449662994508|Location Summary Report_1449662994508.csv
Location Summary Report|000000000001450293231606|Location Summary Report_1450293231606.csv
There data is then sorted with sort /R, where /R defines reverse sort order. The first |-delimited field contains the report name, the second field the zero-padded index number and the third one the original file name. Only such lines are used for copying which hold a report name different to the previous line.
This script handles all your report files that have a report name, a SPACE, a (, an index number and a ) in their file names. It even handles files that do not contain an index number but a report name only in their file names, where they are treated as having an index of 0.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Set up source and destination directories here:
set "SOURCE=D:\source"
set "DESTIN=D:\destin"
> "%~dpn0.tmp" (
for /F "tokens=1,2,3 delims=()" %%I in ('
2^> nul dir /B /A:-D "%SOURCE%\*.csv" ^| ^
findstr /I /R /C:"^[^()]*[^()0-9].csv$" /C:"^[^()][^()]* ([0-9][0-9]*).csv$"
') do (
if "%%J"=="" (
set "REPORT=%%~nI"
set "NUMBER=000000000000000000000000"
setlocal EnableDelayedExpansion
echo(!REPORT!^|!NUMBER:~-24!^|!REPORT!%%~xI
endlocal
) else (
set "REPORT=%%I"
set "NUMBER=00000000000000000000000%%J"
setlocal EnableDelayedExpansion
echo(!REPORT:~,-1!^|!NUMBER:~-24!^|!REPORT!^(%%J^)%%K
endlocal
)
)
)
set "FORMER="
< "%~dpn0.tmp" (
for /F "tokens=1,3 delims=|" %%I in ('
sort /R
') do (
set "REPORT=%%I"
set "ORNAME=%%J"
setlocal EnableDelayedExpansion
if /I not "!REPORT!"=="!FORMER!" (
> nul copy /Y /B "%SOURCE%\!ORNAME!" "%DESTIN%\!REPORT: =!%%~xJ"
)
endlocal
set "FORMER=%%I"
)
)
del /Q "%~dpn0.tmp"
endlocal
exit /B
The related temporary file contains the following unsorted data, based on your sample files:
Sensor|000000000000000000000001|Sensor (1).csv
Sensor|000000000000000000000002|Sensor (2).csv
Sensor|000000000000000000000003|Sensor (3).csv
StartStop|000000000000000000000001|StartStop (1).csv
StartStop|000000000000000000000002|StartStop (2).csv
StartStop|000000000000000000000003|StartStop (3).csv
StartStop|000000000000000000000000|StartStop.csv
The sorting technique is the same as for the other script.
In order to get all the report files you want, you need to execute both scripts.

Batch Command to Move Files from Sub Directories to New Directory

I am attempting to use the batch file below to move files from one folder to another. The batch commands will create sub folders within the destination folder based on the create dates against each file in the source folder.
The problem is that the source folder contains sub folders and the batch commands cannot recurse into sub folders.
Please advise how to modify the batch file to allow recurse into sub folders on the source folder.
Thanks
Rialet
echo %1 "-" %2
If [%1]==[] ECHO "Source Directory parameter required"&GOTO :EOF
If [%2]==[] ECHO "Target Directory parameter required"&GOTO :EOF
SET TrimQuote=%2
for /f "useback tokens=*" %%T in ('%2') do set TrimQuote=%%~T
REM echo %TrimQuote%
::loop through files only
For /F "TOKENS=1 DELIMS=%_TabSpace%" %%B In ('dir %1 /a-d /B /OD') DO (
REM echo "%%B - " %%B
For /F "TOKENS=1 DELIMS=%_TabSpace%" %%D In ('dir %1\"%%B" /a-d /OD ^| findstr /B [0-9][0-9]/[0-9]') DO (
REM echo "%%D - " %%D
for /F "tokens=1,2,3,4 delims=/ " %%b in ("%%D") do (
REM echo "b = " %2\%%c%%a\%%b
REM echo %2\%%d%%c\%%b
if NOT exist %2\%%d%%c\%%b md %2\%%d%%c\%%b
move %1\"%%B" %2\%%d%%c\%%b\
)
)
)
Try using
For /F "TOKENS=1 DELIMS=%_TabSpace%" %%B In ('dir %1 /a-d /S /B /OD') DO (
the /s will cause recursion. The downside is that the output of the dir command is then d:\path\file.ext - which may not marry well with your "TOKENS=1 DELIMS=%_TabSpace%". You'd probably need to use "delims=" (ie, no delimiters, hence entire line in token1).
You can then retrieve the various parts of the full-filename as %%~dB, %%~pB, %%~nB and %%~xB (the drive, path, naem and extension - and you can combine these parts if you wish by using %%~nxB for name+extension, for instance.
Supplemental info - batch commented.
#ECHO OFF
SETLOCAL
:: The above two lines are a traditional batch introduction.
:: The first turns `ECHO`ing of the command to the console OFF
:: The second makes all changes to the environment 'local'
:: which means that any variable changes made during the batch
:: will be undone at the end, restoring the original environment.
:: Note that the official remarks/comments method is
REM This is a remark
:: But the double-colon method is commonly used as :: is less intrusive
:: Echo the two parameters given to the batch (%1 and %2)
echo %1 "-" %2
:: The original parameter-present detection is weak. This is a better method
SET target=%~1
If not defined target ECHO "Source Directory parameter required"&GOTO :EOF
SET target=%~2
If not defined target ECHO "Target Directory parameter required"&GOTO :EOF
:: Note `trimquote` (batch is largely case-insensitive) is a meaningless name
:: New name TARGET is better. Setting to %~2 removes enclosing quotes from
:: string assigned to variable.
::loop through files only
:: `"delims="` means there are no delimiters, so the entire line is assigned to
:: the variable `%%B` (FOR loop variablenames ("metavariables") ARE case-sensitive!)
:: The line being assigned comes from the output of the `DIR` command
:: which is filenames only (/a-d) in subdirectories (/s) in basic form (/b)
:: (ie name only, no dates, sizes, headers or summary) and in order of date (/od)
For /F "DELIMS=" %%B In ('dir "%~1" /a-d /S /B /OD') DO (
REM echo "%%B - " %%B
REM within a FOR loop, better to use REM remarks than :: remarks (version-dependent)
REM I believe the intention of the original here was to pick up the filedate
REM It wouldn't work since FINDSTR is looking for lines that begin (/B) with
REM 2 digits, a slash and one digit, but the date format about to be processed...
REM For /F "TOKENS=1 DELIMS=%_TabSpace%" %%D In ('dir %1\"%%B" /a-d /OD ^| findstr /B [0-9][0-9]/[0-9]') DO (
REM echo "%%D - " %%D
REM Process date - 4 elements separated by space or /. Pick the last three
REM so implictly format is DAYNAME xx/yy/zz BUT the elements would be applied
REM to %%b, %%c, %%d, %%e
REM for /F "tokens=1,2,3,4 delims=/ " %%b in ("%%D") do (
for /F "tokens=1,2,3,4 delims=/ " %%a in ("%%~tB") do (
REM echo "b = " %2\%%c%%a\%%b
REM echo %2\%%d%%c\%%b
REM Make a new directory. 2>nul suppresses error message if already exists
md "%TARGET%\%%d%%c\%%b" 2>nul
move "%%B" "%TARGET%\%%d%%c\%%b\"
)
)
Bit of a nightmare really - No idea of what format date you are using, nor what format target directory structure you want. This should "flatten" the structure, so any file filename.ext would be placed in %target%\xx\yy\zz regardless of where in your source structure the file originally resides. There is also no protection about multiple instances of the same filename.ext with the same DATE but in different subdirectories in the source. Need a lot more clarification of the entire scenario to be more certain. Really just commenting and changing the existing (presumed-working but evidently-faulty) batch...
You can also use the XCOPY function to copy a parent and child folders.

Resources