Copying files with name containing just numbers using a batchfile - windows

I have a Directory with a deep Directory->Sub-directory tree structure. I need to write a batch file to copy all the numbered files (files with names as digits and no alphabetic characters) from all the sub-directories.
For example, a sub-directory might contain the following files:
WR10091.txt
AX10091.htm
10091.txt
AX10091.xml
10091.xml
I need to copy 10091.txt and 10091.xml to another location. I can copy files like AX10091.xml and AX10091.htm by specifying AX*.*. But I cannot figure out how to copy just numbered files with no alphabetic characters. There are thousands of directories and the directory structure does not have any pattern (the depth of a tree branch can vary considerably).
Any help will be appreciated.

#echo off
setlocal enableextensions disabledelayedexpansion
set "source=%cd%"
set "target=x:\target\folder"
for /r "%source%" %%a in (*) do (
(for /f "delims=0123456789" %%b in ("%%~na") do (
break
)) || echo copy "%%~fa" "%target%"
)
In this code the for %%a will iterate over all the files under the indicated folder. For each of them, the for /f %%b will try to tokenize the file name (%%~na) using numbers as delimiters. If the file name only contains numbers, there will be nothing to process (only delimiters) and the inner for raises errorlevel. This is checked with conditional execution (the code after the || is executed if the previous command fails) and if errorlevel was raised the copy operation is echoed to console.
If the output is correct, remove the echo to perform the copy.
note: the break in the inner for loop is included just to have a command that does nothing when files with non numeric names are found.

#echo off
for /f "tokens=* delims=" %%a in ('dir /b /s /a:-d "*"') do (
echo %%~na|findstr /e /b /r "[1-9]*" >nul 2>nul && (
copy %%~fa c:\somewhere\
)
)
should be executed in the same directory as the files.

for /f "delims=" %%a in ('dir /b/s/a-d ^| findstr /reic:"\\[0-9][0-9]*\..*" /c:"\\[0-9][0-9]*"') do copy "%%~a" "targetDir"
This might not work with XP and/or Vista, but this can be fixed if needed (see What are the undocumented features and limitations of the Windows FINDSTR command).

Related

Batch rename multiple files with hyphenated names

I have a series of files that I download/process regularly and need to use a batch file to rename. Each filename is something like word-word-word-datetime.csv. There is always a '-' between words and always -datetime before the '.csv' file extension. I need to remove the -datetime so that the files are named word-word-word.csv. In some cases there might be just one word before the -datetime but there can be a string of many words as well. I download these files and move them to a specific folder for processing, and there is already a batch file in the folder that I need to modify to also rename the files.
For example, I need the filenames below:
this-is-a-file-20200804134809.csv
another-file-20200804134750.csv
some-other-file-20200804134699.csv
file-20200804134389.csv
To be renamed to:
this-is-a-file.csv
another-file.csv
some-other-file.csv
file.csv
This answer is almost exactly what I need, but I'm not familiar enough with the syntax to modify it for renaming files with multiple hyphenated words (code from linked answer copied below).
#echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.mp4"') do (
echo move "%%a-%%b" "%%a%%~xb"
)
I was able to rename all of the .csv files in my folder, truncating the name to remove the last 15 characters as suggested by #Compo.
#echo off
setlocal enabledelayedexpansion
for %%f in (*.csv) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-15!%%~xf"
)
Here's a more robust example, using the advice I provided in the comments:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "EOL=| Delims=" %%G In ('Dir /B /A-D *-??????????????.csv ^
^|"%__AppDir__%findstr.exe" /IR ^
"\-19[0123456789]*\.csv$ \-20[0123456789]*\.csv$"') Do (
Set "BaseName=%%~nG"
SetLocal EnableDelayedExpansion
Ren "%%G" "!BaseName:~,-15!%%~xG"
EndLocal
)

Batch: For directories not working with delayed expansion

I have a CI system that regularly publishes new files.
In a script, I want to take the latest version of several (separately published) files. The general file structure is the following: C:\Path\to\publish\<token>\<flavor>\file. There are 3 variable parts in this, per published file. (And all 3 can in some cases have wildcards in it)
The setup I currently have, makes use of delayed expansion to make this configurable:
set A_PATH=C:\Path\to\publish\*
set A_SUBDIR=<flavor>
set A_NAME=file_*.txt
call :searchFile A
goto :logicUsingA
:searchFile
SETLOCAL ENABLEDELAYEDEXPANSION
set SEARCH_PATH=%~1_PATH
set SEARCH_NAME=%~1_NAME
set SEARCH_SUBDIR=%~1_SUBDIR
FOR /D %%I IN ('DIR !%SEARCH_PATH%! /O:-D /B') do (
echo I:%%I
FOR %%J IN (!%SEARCH_SUBDIR%!) do (
echo J:%%J
FOR /F "delims=" %%K IN ('DIR %%I\%%J\!%SEARCH_NAME%! /A:-D /O:-D /B') do (
echo K:%%K
ENDLOCAL
set "%~1=%%I\%%J\%%K"
GOTO :EOF
)
)
) 2> nul
This system almost did what it needed to do, namely, based on a configuration pick some files and fill a variable with them.
Unfortunately, we discovered that something goes wrong with executing this script.
The FOR /D %%I IN ('DIR !%SEARCH_PATH%! /O:-D /B') should sort the directories with the newest first. However, the print below it, prints: (assume 001 was created before 002 ...)
I:'DIR
I:C:\Path\to\publish\001
I:C:\Path\to\publish\002
I:C:\Path\to\publish\003
This makes me suspect that the delayed expansion doesn't play nice with the for-loop, as this is the only noticeable difference with other solutions I find at stackoverflow, like https://stackoverflow.com/a/45104510/2466431
Would it be possible to instruct the for-loop to execute this dir-command iso treating it as a list of items?
EDIT: What the code is trying to achieve is a windows batch variant of the bash one-liner: ls -t C/Path/to/publish/*/<flavor>/file_*.txt
The loop for /D%%I in ('dir !%SEARCH_PATH%! /O:-D /B') do ( ... ) is wrong as you must use for /F to capture and parse the output of a command like dir in our situation. So you may either use for /D %%I in ("!%SEARCH_PATH%!") do ( ... ) or for /F "delims=" %%I in ('dir "!%SEARCH_PATH%!" /O:-D /B') do ( ... ) to be syntactically correct.
But neither will probably be useful for you, because:
for /D does not access the file system unless wildcards are present;
for /D does not provide sort options (it returns directories as it gets them from the file system);
dir, when a directory name is given, lists its contents rather than just returning its name;
Nevertheless, there is a way to work around the said problem with dir, namely appending \ and trying to list the contents of the given directory; if wildcards are given, dir returns an error; if a dedicated directory name is given but it does not exist, dir returns an error too; conditional execution can be applied to make use of that behaviour:
dir /B /A:D "%SomeDir%\" && (echo "%SomeDir%") || dir /B /A:D /O:-D /T:C "SomeDir%"
This provides the following results:
if %SomeDir% points to a dedicated existing directory, the first dir command lists its contents, so it succeeds, hence the echo command is executed, returning the (quoted) variable contents, but the second dir command is skipped; (quotation of the echo string is done here in order to protect whitespaces or other special characters; the surrounding quotation marks "" are no problem though as they can easily be removed later anyway;)
if %SomeDir% points to a dedicated directory that cannot be found, the first dir command fails (error: The system cannot find the file specified.), hence the echo command is skipped, but the second dir command is executed, which also fails (error: File Not Found);
if %SomeDir% contains a wildcard in the last path element, the first dir command fails (error: The filename, directory name, or volume label syntax is incorrect.), hence the echo command is skipped, but the second dir command is executed, which lists all matching directory names, sorted by creation date/time (newest first);
The listing returned by the first dir command as well as any error messages can be suppressed using redirection, so the remaining output is the one by echo or by the second dir:
> nul 2>&1 dir /B /A:D "%SomeDir%\" && (echo "%SomeDir%") || 2> nul dir /B /A:D /O:-D /T:C "SomeDir%"
This can now be applied to your script:
#echo off
set "A_PATH=C:\Path\to\publish\*"
set "A_SUBDIR=<flavor>"
set "A_NAME=file_*.txt"
call :searchFile A
echo A:%A%
rem goto :logicUsingA
goto :EOF
:searchFile
set "%~1="
setlocal EnableDelayedExpansion
set "SEARCH_PATH=%~1_PATH"
set "SEARCH_NAME=%~1_NAME"
set "SEARCH_SUBDIR=%~1_SUBDIR"
cd /D "!%SEARCH_PATH%!\.." || exit /B 1
for /F "delims=" %%I in ('
^> nul 2^>^&1 dir /B /A:D "!%SEARCH_PATH%!\" ^
^&^& ^(echo "!%SEARCH_PATH%!"^) ^
^|^| 2^> nul dir /B /A:D /O:-D /T:C "!%SEARCH_PATH%!"
') do (
echo I:%%~nxI
for /F "delims=" %%J in ('
^> nul 2^>^&1 dir /B /A:D "%%~nxI\!%SEARCH_SUBDIR%!\" ^
^&^& ^(echo "!%SEARCH_SUBDIR%!"^) ^
^|^| 2^> nul dir /B /A:D /O:-D /T:C "%%~nxI\!%SEARCH_SUBDIR%!"
') do (
echo J:%%~J
for /F "delims=" %%K in ('
^> nul 2^>^&1 dir /B /A:-D "%%~nxI\%%~J\!%SEARCH_NAME%!\" ^
^|^| 2^> nul dir /B /A:-D /O:-D /T:C "%%~nxI\%%~J\!%SEARCH_NAME%!"
') do (
echo K:%%K
endlocal
set "%~1=%CD%\%%~nxI\%%~J\%%K"
goto :EOF
)
)
)
goto :EOF
In the inner-most loop that enumerates files rather than directories, I applied the same technique, but I skipped the echo command as it should never be executed anyway; the only reason why I kept the two dir commands here is to handle the situation when you specify a certain file name (no wildcards) but there is actually a directory with that name (a single dir would then unintentionally return its contents).
There have also a few other things changed:
the variable %~1 is initially cleared (in the sub-routine :searchFile, just in case no file could be found at all);
the quoted set syntax is consistently used (set "VAR=Value");
all paths are now quoted to avoit trouble with whitespaces or other special characters;
the dir option /T:C is added to regard the creation date/time rather than the date/time of the last modification; just remove it if you want to regard the latter;
cd /D"!%SEARCH_PATH%!\.." ||exit/B 1 has been added to change to the parent directory, because the later used dir /B commands just return directory or file names but not full paths; that is also the reason why the ~nx-modifier is used in %%~nxI, so there is no more difference whether the value comes from dir /B or echo; the exit /B 1 part makes sure to skip the remaining code in case the parent directory could not be found/accessed;
~-modifiers are used in %%~J to ensure unquoted directory names (remember that I put quotes in the echo command lines);
2> nul has been removed (from the end of the body of the outer-most loop) to not suppress error messages in general;

(Batch) How to recursively delete all files/folders in a directory except those with a leading .?

I have a directory, src. I want to recursively delete all of its contents except for files (.gitignore, ...) and folders (.git, .vscode, ...) whose names begin with .. Matching that pattern in subdirectories is neither necessary nor harmful.
What is the cleanest way to do this in a batch file?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
:: step 1 : delete all files NOT starting "."
FOR /f "tokens=1*delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*" '
) DO (
ECHO %%~nxa|FINDSTR /b /L "." >nul
IF ERRORLEVEL 1 ECHO(DEL "%%a"
)
:: step 2 : delete all directories NOT starting "."
FOR /f "tokens=1*delims=" %%a IN (
'dir /s /b /ad "%sourcedir%\*" ^|sort /r'
) DO (
ECHO %%~nxa|FINDSTR /b /L "." >nul
IF ERRORLEVEL 1 ECHO(RD "%%a"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
The required RD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(RD to RD to actually delete the directories.
for each filename in the entire subtree, see whether it starts with ., setting errorlevel to non-0 if not and hence delete the file.
Once this has been done, repeat the operation with directorynames, but sort the names found in reverse so that a subdirectoryname of any directory will appear before the directoryname. Attempt to remove the directory with an rd - it will remain if it contains files or subdirectories (which implicitly will start .). Append 2>nul to the rd line to suppress error messages (where the directory cannot be removed as it still contains files/subdirectories)
Try this to exclude folders with a leading dot (on a per folder base):
for /f "tokens=*eol=." %%A in ('dir /B /AD') do Echo=%%A
This doesn't affect folder names containing dots at other positions.
A relatively slow variant recursing all folders and using findstr to filter all folders containing \. both chars need escaping with a backslash
for /r /D %%A in (*) do #echo %%A|findstr /V "\\\."

Batch file to find multiple string using findstr and copy files to a folder

I am attempting to find multiple strings in files in a directory, there are thousands. I currently run the following command to search the directory:
findstr /s "customerid" *
Now this allows me to find the file that contains that string. I normally have two pieces of information a customer id and an event type. One customer can have up to 30 associated event such as "website registration".
What I would like to do is, search the directory for both the customer id and the event. Then copy the file to a new location. Is this possible in a batch file?
Supposing you want to find all files that contain both words (customer and event in this example), you could use the following script:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "PATTERN=*.txt"
set "SOURCE=."
set "TARGET=D:\Data"
set "STRING1=customer"
set "STRING2=event"
pushd "%SOURCE%" && (
for /F "delims=" %%F in ('findstr /S /M /I /R /C:"\<%STRING1%\>" "%PATTERN%"') do (
for /F "delims=" %%E in ('findstr /M /I /R /C:"\<%STRING2%\>" "%%F"') do (
ECHO copy "%%E" "%TARGET%\%%~nxE"
)
)
popd
)
endlocal
exit /B
After having tested the script, remove the upper-case ECHO in front of the copy command!
#echo off
setlocal enableextensions disabledelayedexpansion
set "sourceFolder=x:\somewhere"
set "targetFolder=y:\another\place"
set "customerID=00000000"
set "event=eeeeeeeeee"
for /f "delims=" %%a in ('
findstr /m /s /l /c:"%customerID%" "%sourceFolder%\*"
^| findstr /f:/ /m /l /c:"%event%"
') do (
ECHO copy "%%~fa" "%targetFolder%"
)
findstr can deal with it. We only need two instances
The first one will search all the input files for the first string, returning only the list of matching files. This list of files will be piped into the second instance
The second one will search the second string but only in the files found by the first instance, reading the list of files where to search from standard input stream (/f:/)
The rest of the code is just a for /f wrapping the two findstr commands to process the output of the second one and do the file copy.
After having tested the script, remove the upper-case ECHO in front of the copy command!

List all files with 2 timestamps, size, but without path or dir

Windows, Command Prompt, need to generate a .txt file output containing of all files from a big and complex dir tree with one (1) line for each files as:
CreationDateYYYYMMDD-HHMMSS, LastModifiedYYYYMMDD-HHMMSS, filesize[no K commas], filename.ext
for example:
20100101-174503, 20120202-191536, 1589567, myfile.ext
The list should not contain lines of dir name entries, etc., only filenames, even if the same file is present in more than once. Time in 24 hours format.
dir /s/t:c/t:w/-c > filelist.txt
command does not exactly works this way.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\program files"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*" '
) DO (
FOR %%d IN (timewritten timecreated) DO SET "%%d="
FOR %%k IN (-d s h) DO (
IF NOT DEFINED timewritten FOR /f "tokens=1,2 delims= " %%d IN ('dir /tw %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timewritten=%%d %%e"
IF NOT DEFINED timecreated FOR /f "tokens=1,2 delims= " %%d IN ('dir /tc %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timecreated=%%d %%e"
)
ECHO !timecreated! !timewritten! %%~za %%~nxa
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
Interesting problem. This code processes it by
First, applying the standard directory-list for filenames on the tree from the relative root (%sourcedir%) to %%a
Using the full filename in %%a, set timewritten and timecreated from an ordinary dir list targeting the file in question.
It appeared that %%~ta didn't play nicely to extract the timestamp for hidden and system files, so I decided to build them from the ordinary dir listing with the appropriate t setting, specifically listing with /a-d, /as and /ah and filtering for the line which matched the filename, which seemed to extract the data appropriately.
I left the date/time in raw format. It should be an easy task to extract the various elements and construct the report in the format you want.
This question is a dupe of the SO post cmd dir /b/s plus date, but posting what worked for me:
#echo off
REM list files with timestamp
REM Filename first then timestamp
for /R %I in (*.*) do #echo %~dpnxI %~tI
#echo off
REM list files with timestamp
REM Timestamp first then name
for /R %I in (*.*) do #echo %~tI %~dpnxI
The above are the versions that you would directly paste into a command prompt.
If you want to use these in a batch file and log the output, you could do something like:
rem: Place the following in a batch file such as DirectoriesBareWithTS.cmd.
rem: As the first step in the batch file, net use to the directory or share you want the listing of
rem: Change to your target directory
Y:
for /R %%I in (*.mp4) do #echo %%~tI %%~dpnxI
Then you can pipe the output to a log file when you execute:
DirectoriesBareWithTS.cmd > C:\temp\GiantLongDirListing.log
You could then import that log into Excel.

Resources