Searching for files in directory by DATE using cmd [closed] - command-line

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to create a bat file that copies all jpeg image files created on a specific date - eg 2013/05/05.
It will go something like this:
xcopy g:\DCIM\images\'command for date'*.jpg c:\users\david\images\newImages
How do I accomplish that?
I tried the following but it copied all the images in the folder and ignore the date param:
xcopy /l /s /d:05-05-2013 g:\DCIM\images\*.jpg c:\users\david\images\newImages

Try this:
#echo off
setlocal
for /f "tokens=1,5 delims= " %%a in ('dir /a-d /tc G:\DCIM\images\*.jpg') do (
if %%a equ 2013/05/05 copy "g:\DCIM\images\%%b" "c:\users\david\images\newImages"
)

try this:
#echo off&setlocal
cd /d "g:\DCIM\images"
for /f "tokens=3" %%i in ('dir ^|findstr "^[0-9]"') do set "AMPM=%%i"
if "%AMPM:M=%"=="%AMPM%" (set "AMPM=3") else set "AMPM=4"
for /f "tokens=1,%AMPM%*" %%i in ('dir /a-d /tc *.jpg') do if "05-05-2013"=="%%i" echo copy "%%k" "c:\users\david\images\newImages"

Related

How can I compare two dates in batch?

I have a batch script to get two dates; one of a folder, and the current system date.
I want to specifically compare the two by seeing if the date of the folder is 10 minutes or less older than the current date. This essentially checks if the user has modified this folder at the most 10 minutes ago.
Here's my current code (not complete, but the base):
#echo off
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%a/%%b/%%c)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a:%%b)
setlocal enabledelayedexpansion
if exist "C:\$Recycle.Bin" (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{recycle-bin-date}=%%a")
for %%a in ("!{recycle-bin-date}!") do (
Set "data=%%~ta"
)
popd
)
set date1=%mydate% %mytime%
set date2=!data!
echo Date 1 (Current): %date1%
echo Date 2 (Recycle): %date2%
pause
::We have the dates above, how do I achieve what I'm trying to do?
If anyone could help me here, I'd really appreciate it.
You CAN do date time math in pure batch but it is quite cumbersome.
(See Ritchie Lawrence batch function library)
I recommend to use PowerShell as a tool for this
PowerShell one liner:
[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes
Wrapped in a batch
#Echo off
For /f "usebackq" %%A in (`
Powershell -NoP -C "[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes"
`) Do Set "AgeMinutes=%%A"
Echo Age in minutes %AgeMinutes%

how to replace special characters in file names [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
We are in the process of moving to OneDrive for Business. To store files in OneDrive you can't have the following charactes in the filename:
/:*?"<>|#%
Additionally, a file name that begins with a tilde (~) isn't supported.
I would like to search and replace the special character with a dash.
Anyone have a batch file or powershell script?
Coincidentally, \ / : * ? " < > | aren't allowed in Windows filenames either, so most of your list is already a non-issue. Assuming that list of characters is complete, all that remain are hashes, percents, and leading tildes.
#echo off
setlocal
:: replace ~ only if first char of filename
for %%I in ("~*") do (
set "file=%%~I"
setlocal enabledelayedexpansion
echo %%~I -^> -!file:~1!
ren "%%~I" "-!file:~1!"
endlocal
)
:: replace # or % everywhere in filename
for %%d in (# %%) do (
for %%I in ("*%%d*") do (
set "file=%%~I"
setlocal enabledelayedexpansion
echo %%~I -^> !file:%%d=-!
ren "%%~I" "!file:%%d=-!"
endlocal
)
)
But as Dour points out, this only fixes some of the problems. Your file uploads might still require some hand-holding. Or who knows? This could solve all your worldly problems. shrug
Edit: O.P. asked about adding /r to the for loops to make the replacements recursive. You could do that with a few tweaks, but you'll end up looping through the file list 3 times -- once for each symbol you're replacing. I suggest this would be a more efficient approach:
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" goto usage
if not exist "%~1" goto usage
pushd "%~1"
for /r %%I in (*) do (
set "file=%%~nxI"
if "!file:~0,1!"=="~" (
set "file=-!file:~1!"
)
for %%d in (# %%) do (
if not "!file!"=="!file:%%d=!" (
set "file=!file:%%d=-!"
)
)
if not "!file!"=="%%~nxI" (
echo %%~fI -^> !file!
ren "%%~fI" "!file!"
)
)
goto :EOF
:usage
echo Usage: %~nx0 pathname
echo To operate on the current directory, use a dot as the pathname.
echo Example: %~nx0 .
Edit 2: Added argument syntax.

Batch file for listing all files from a date interval

I need to do a batch file which order all files by a date interval, example:
#echo off
echo Input the date(dd/mm/yyyy):
set /p compDate=
::After that I will compare from the actual day (%date%), example:
set interval = %compDate% - %date%... *Something like that*
::After that I need to list all files from a specific directory, example:
echo Input the directory:
set /p directory=
SET Exit= %UserProfile%\Desktop\test.txt
::After that I might need dir /tc to get the creation date, example:
pushd "%directory%"
dir /s /tc /a-d > %Exit%
::After that I don't know how to get only the lines which are in date interval, example:
Today is 19/08/2014, but I want to search all files created from day 10/07/2014.
So I have to copy all lines which have the date 10/07/2014, 11/07/2014, 12/07/2014 and so on until stop on today created files.
I tried with findstr, but I can't set the date interval, just a specific date to search in the .txt created.
Somebody know how to do that?
If I correctly understood the request, you really don't want files created in a given interval, but files created after a given date. The Batch file below assume that the date used by the system appear in DD/MM/YYYY order:
EDIT: Some modifications as reply to the comments
#echo off
setlocal EnableDelayedExpansion
echo Input the date(dd/mm/yyyy):
set /p compDate=
for /F "tokens=1-3 delims=/" %%a in ("%compDate%") do set compDate=%%c%%b%%a
echo Input the directory:
set /p directory=
SET Exit=%UserProfile%\Desktop\test.txt
pushd "%directory%"
(for /F "tokens=1-5*" %%a in ('dir /s /od /tc /a-d') do (
set "fileDate=%%a"
if "!fileDate:~2,1!!fileDate:~5,1!" equ "//" (
for /F "tokens=1-3 delims=/" %%x in ("!fileDate!") do set fileDate=%%z%%y%%x
if !fileDate! geq %compDate% (
set "fileSize= %%e"
echo %%a %%b %%c %%d !fileSize:~-16! %%f
)
)
)) > %Exit%
popd
A solution that uses WMIC and is independent from time/date settings:
#echo off
setlocal
set /p compDate=Input the date(yyyymmdd):
set /p directory=Full directory path (with no slash at the end):
set exit_file= %UserProfile%\Desktop\test.txt
break>%exit_file%
for /f "tokens=1,2 delims=:" %%a in ("%directory%") do (
set "dir_drive=%%~a:"
set "dir_path=%%~b\"
)
set dir_path=%dir_path:\=\\%
setlocal enableDelayedExpansion
for /f "useback skip=1 tokens=1,2* delims=:" %%f in (`" wmic datafile where (drive='!dir_drive!' and path like '%dir_path%') get CreationDate^,name"`) do (
set creation_date=%%f
set creation_date=!creation_date:~0,8!
set "file_name=%dir_drive%%%~g"
if 1!creation_date! GTR 1%compDate% (
echo !file_name!>>%exit_file%
)
)
exit /b 0

Batch file to find and run latest file in a dir with only partially known name?

How can I write in a simple Windows 7 compatible Batch file:
Where filename begins with "c:\my folder\myfile*.exe", run only the most recent one created.
For example, if I have 10 files in "c:\my folder\" and they are all similarly named myfile*.exe, and myfileBOB.exe was the last of this named file to be created - how to fish this out (the folder also contains other general files of different types) automatically by filename myfile* AND created date to execute?
Many thanks!
Sort the files by date ascending, and keep the last (most recent) one.
#echo off
setlocal
pushd "c:\my folder"
set "file="
for /f "eol=: delims=" %%F in ('dir /b /a-d /od myfile*.exe') do set "file=%%F"
if defined file "%file%"
popd
Or sort files by date descending, and break out of loop after first iteration.
#echo off
pushd "c:\my folder"
for /f "eol=: delims=" %%F in ('dir /b /a-d /o-d myfile*.exe') do "%%F"&goto :break
:break
popd

Batch script that separates that creates folder by month

#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM Set variables
SET SOURCE=C:\My WebEx Recordings
SET DEST=\\XXXRD12\c$\WebExVideoArchive
SET 7ZIP=C:\Program Files\7-Zip\7z.exe
REM Compress local files with 7zip
ECHO ---------------------------------------------------------
ECHO BEGINNING VIDEO COMPRESSION OPERATIONS
ECHO ---------------------------------------------------------
CD /D "%SOURCE%"
FOR %%f in ("*.wrf") DO (
SET FILENAME=%%~nf
ECHO Compressing !FILENAME!
"!7ZIP!" a -t7z -aoa "!FILENAME!.7z" "%%f"
)
REM Copy compressed files
ECHO ---------------------------------------------------------
ECHO COMPRESSION COMPLETE - BEGINNING COPY OPERATIONS
ECHO ---------------------------------------------------------
REM XCOPY <source> <destination> <options>
XCOPY "%SOURCE%\*.7z" "%DEST%" /Y /V /I /R
REM Confirm successful copy, then delete originals
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Originals...
DEL /Q "%SOURCE%\*.*"
)ELSE (
ECHO Error Detected During Copying. Please try again...Press Any Key to Exit
Pause
)
I am trying to edit this code to when we run the batch file it creates a folder based off the month of our file format. We ran this script a whole lot but forgot to make a folder called April now we have May mixed in with April.
The files are formatted like
Username-R705-2011.05.04-1601-Disconnected.7z
I was wondering if there is anyway it can go off the .04 and make a folder for that month so it will automatically put it in the folder it is needing to go into, so it will be easy to search for by month.
** would it be possible if we can not use the format that we format our files in to have it sort by file creation.
Erase everything below (and including) the line REM XCOPY <source> <destination> <options> and replace it with the following:
for /F "usebackq delims=" %%a in (`dir /b "%SOURCE%\*.7z"`) do (
SET CURRENT_FILE=%%a
REM Extract the month.
for /F "usebackq tokens=3 delims=-" %%i in ('!CURRENT_FILE!') do (
SET CURRENT_FILE_DATE=%%i
SET FILE_MONTH=!CURRENT_FILE_DATE:~-2!
SET MONTH_DEST=!DEST!\!FILE_MONTH!
)
XCOPY "%SOURCE%\!CURRENT_FILE!" "!MONTH_DEST!\" /Y /V /I /R
REM Confirm successful copy, then delete original
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Original...
DEL /Q "%SOURCE%\!CURRENT_FILE!"
)ELSE (
ECHO Error while copying "%SOURCE%\!CURRENT_FILE!.
)
)
This code goes through every file matching %SOURCE%\*.7z, extracts the month, and then copies the file to %DEST%\<month>. Month is just the 2-digit number from the filename.
Seems like you have a folder full of files with different numeric-month values embedded in the filename. I was thinking of extracting that value from each file, but it would be more straightforward to use brute force wildcards with 12 different XCOPY commands:
XCOPY "%SOURCE%\*-*-20??.??.01-*.7z" "%DEST%\01" /Y /V /I /R
XCOPY "%SOURCE%\*-*-20??.??.02-*.7z" "%DEST%\02" /Y /V /I /R
[...]
XCOPY "%SOURCE%\*-*-20??.??.12-*.7z" "%DEST%\12" /Y /V /I /R
I may be misunderstanding the question but if you can make some assumptions about the filenames, this would work.