Ok let me explaned it again
I have two location :
F:\Reporting\02.2013 in the folder "02.2013" I have below files
Balance Sheet_20130228_045485.xls 3/22/2013 2:40 PM
Balance Sheet_20130228_024867.xls 3/23/2013 1:40 PM
Balance Sheet_20130228_023556.xls 3/23/2013 3:50 PM
F:\Statements\02.2013 in the folder "02.2013" I have below files
FS_20130228_045485.xls 3/22/2013 4:40 PM
FS_20130228_024867.xls 3/23/2013 1:40 PM
FS_20130228_023556.xls 3/23/2013 6:45 PM
First I wants to moov the latest modified files from both the folder to Target folder :
Target folder is F:\accounting\02.2013
Then I wants to rename those files as
Balance Sheet_20130228_023556.doc as BalanceMTD.xls
FS_20130228_045485.doc as FS.xls
note: I have to do this activity every month,
so folder location will be change like F:\Reporting\03.2013
and file name also will be change like FS_20130331_045465.doc 4/27/2013 4:30 PM
Can you please suggest me any batch file which can help me to do this.
I'll change the date every month if required before execute it.
This will move and rename the newest file from each of your source directories:
#echo off
setlocal
set DateFolder=02.2013
set TargetFolder=F:\Accounting\%DateFolder%
:: Move the newest file from Reporting and rename it to BalanceMTD.xls
call :MoveAndRename "F:\Accounting\%DateFolder%" "%TargetFolder%\BalanceMTD.xls"
:: Move the newest file from Statements and rename it to FS.xls
call :MoveAndRename "F:\Statements\%DateFolder%" "%TargetFolder%\FS.xls"
:: Done
goto :eof
:MoveAndRename
set SourceFolder=%~1
set TargetFile=%~2
:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do set "NewestFile=%%F"
:: Move and rename it to the target
move "%SourceFolder%\%NewestFile%" "%TargetFile%"
:: Done with this subroutine
goto :eof
I've put the bulk of the working code in a "subroutine" named MoveAndRename, which simply finds the newest file in a particular folder (using a for /f loop over a dir /a-d to loop over all files in order of date, remembering only the last one), then a move to move and rename it to the target. (If you want to keep the original file, then do copy instead.)
The top part of the batch file then just calls the MoveAndRename subroutine multiple times, once for each source folder you want to look at.
If you want to avoid editing the batch file every month, change the third line to this:
set DateFolder=%1
and pass the date to the batch file as an argument: MonthlyProcess.bat 02.2013. Or you can set DateFolder using the %date% environment variable, but since it is formatted based on your locale settings, and it is well documented elsewhere, I will leave that as an exercise for you.
#echo off
setlocal EnableDelayedExpansion
cd F:\MY DOCUMENTS\zyx
rem Process all file names
for /F "delims=" %%a in ('dir /B *.xls') do (
rem Get base name before first underscore (ie: "Balance Sheet" or "FS")
for /F "delims=_" %%b in ("%%a") do (
rem Check if name have two words (ie: "Balance"/"Sheet" or "IC"/"Activities")
for /F "tokens=1,2" %%c in ("%%b") do (
rem If base name have just one word...
if "%%d" equ "" (
rem New name is that word (ie: "FS")
set newName=%%c
) else (
rem We don't know what goes here!
)
)
)
ren "%%a" "!newName!.xls"
)
We could complete previous program if you give us details on how to do so...
Related
Found a pyhton solution here, but I need a batch file-based solution.
Have many files:
SSP4325_blah-blah-blah.xml
JKP7645_blah.xml
YTG6457-blah-blah.xml
And folder names that contain a piece of the file name:
RefID - SSP4325, JKP7645, GHT1278, YRR0023
RefID - YTG6457
I'm looking for a batch solution which would read a portion of the file name at the front (before either the first dash or underscore) and then move that file into the folder where the front of the filename exists as part of the folder name.
So in the above examples, the first two files (SSP4325 and JKP7645) were moved into the first folder because it contained it contained that text as part of the folder name.
The third file would be moved into the second folder.
I have hundreds of files and 63 folders. So I'm hoping to be able to automate.
Can't use Powershell or Python due to limitations of the environment. So hoping for a batch file approach.
Thanks. Sean.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.xml" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%\*%%b*" '
) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
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)
After establishing the directories, the outer loop puts the filename in %%a, the next loop gets the first part of that name, up to but not including the first - or _ (the delims specified) into %%b.
The inner loop finds the target directory containng %%b in the destination directory and constructs an appropriate move line.
This solution review the folders just one time and store they in an array, so this method should run faster.
#echo off
setlocal EnableDelayedExpansion
rem Process the folders
set i=0
for /D %%a in (*) do (
rem Store this folder in the next array element
set /A i+=1
set "folder[!i!]=%%a"
rem Separate folder in parts and store the number of the array element in each one
for %%b in (%%a) do set "part[%%b]=!i!"
)
rem Process the files
for %%a in (*.xml) do (
rem Get the first part of name
for /F "delims=-_" %%b in ("%%a") do (
rem If such a folder exists...
if defined part[%%b] (
rem Get the number of the corresponding array element and move the file
for %%n in (!part[%%b]!) do ECHO move "%%a" "!folder[%%n]!"
) else (
echo No folder exists for this file: "%%a"
)
)
)
This method have also several advantages: you may check if a certain folder does not exists, or get the number of files moved to each folder, etc. If you are not interested in these points, just remove the if command and make the code simpler...
An explanation of array management in Batch files is given at this answer.
Not sure this is possible with only a batch file.
I have a file named BaseFile.7z location is E:\Backup\C Drive Zip\BaseFile.7z
Is it possible to create a batch command that renames the file with its creation date? For example BaseFile - 02-19-2015.7z
I currently have a command that renames the file with the current date which I pasted below for reference, but thats not exactly what I'm looking for. I need creation date.
RENAME "E:\Backup\C Drive Zip\Jaipur.txt" "BaseFile - %date:/=-%.txt"
#ECHO OFF
SETLOCAL
SET "filename=U:\sourcedir\zzz.zzz"
IF NOT EXIST "%filename%" ECHO "%filename%" NOT found&GOTO :eof
SET "datepart="
FOR /f "tokens=1-3delims=/-:" %%a IN ('dir /tc "%filename%"') DO IF "%%c" neq "" SET "datepart=%%a-%%b-%%c"
FOR /f %%a IN ("%filename%") DO FOR /f %%d IN ("%datepart%") DO ECHO(REN "%%a" - "%%~na %%d%%~xa"
GOTO :EOF
The required REN command is merely ECHOed for testing purposes. After you've verified that the command is correct, change ECHO(REN to REN to actually rename the file.
Note that there is general sloppiness in the use of date-references. There are three dates on each file - actual create date (use /tc), last access (/ta) and last-written (/tw).
The process locates the file, then reads a dir listing with the appropriate date selected. The only or last line in the listing that will contain a third non-empty token is the date/time of the file in question, so datepart will acquire yyyy-mm-dd hh
the for/f %%a then applies the full filename to %%a ready for partitionig into its components and the for/f %%d assigns the first token from datepart (ie up to the space) into %%d.
Bang the components together, and the resut is reported...
I need to move certain files to another folder based on modified date.
I came up with the following batch script for the move.
#ECHO OFF
SetLocal EnableDelayedExpansion
set "FolderA=D:\IN\Set1"
set "FolderB=D:\IN\Set2"
set "FolderC=D:\OUT\"
for /f "tokens=2 delims=," %%a in (D:\input.txt) do (
Xcopy "!FolderB!\%%~a" "!FolderC!\"
Echo Copied "%%~a" to "!FolderC!"
)
for /f "tokens=1 delims=," %%a in (D:\input.txt) do (
Xcopy "!FolderA!\%%~a" "!FolderC!\"
Echo Copied "%%~a" to "!FolderC!"
)
The input.txt contains file names as:
file1.dat,file_abcd_efgh_ijklmnop02-2014.dat
file2.dat
file3.dat
The destination, FolderC, should actually be set as D:\OUT\2014\P02 which is based on file_abcd_efgh_ijklmnop02-2014.dat --> (2014, 02).
While moving from FolderB to FolderC, we should check if file has modifed date within past 34 days. If so, and the file name is file_abcd_efgh_ijklmnop02-2014.dat, check if a folder D:\OUT\2014\P02 exists, else create it.
While moving from FolderA to FolderC, the files should be checked for modifed date within past 34 days, and should be moved to D:\OUT\2014\P02.
Thanks.
I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
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 gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")
I understand that there are tons of questions on this site regarding the creation of a batch file that goes through the file in a specified folder and deletes them if it satisfies the condition stated.
However, I would like to tweak that a little bit. In my batch file, I would like to look at a folder, say C:\Dev and get all the files that are within the same month. After getting all those files, I want to sort through all the dates and delete everything except for the latest one. So if I have 5 files for January on that folder with dates January 1, 12, 20, 27, and 30, I would only keep the file dated January 30th and delete all the others.
Is this possible?
< lang-dos -->
#ECHO OFF
SETLOCAL
SET "targetdir=c:\sourcedir"
SET "pfname="
PUSHD "%targetdir%"
FOR /f "delims=" %%a IN ('dir /b /a-d /o:d "*" ') DO (
SET "fname=%%a"
SET "fdate=%%~ta"
CALL :process
)
POPD
GOTO :EOF
:process
:: reformat date - this depends on yout local date-format.
:: YY(YY)MM required - my format is dd/mm/yyyy
SET fdate=%fdate:~6,4%%fdate:~3,2%
IF NOT DEFINED pfname GOTO nodel
IF %fdate%==%pfdate% ECHO DEL "%targetdir%\%pfname%"
:nodel
SET pfdate=%fdate%
SET "pfname=%fname%"
GOTO :eof
This should work for you. 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.
First, the target directory is set up and pfname is cleared.
The PUSHD changes the current directory until the POPD is executed
the dir command outputs filenames only (/b), no directory names (/a-d) in date-order (/o:d). Each line sets fname to the filename and fdate to the filedate.
within :process, the date-string is manipulated. I don't know which format you use, but the basic formula is %variable:~startposition,length% where startposition starts at 0=first character. The idea is to have fdate in the format yyyymm
if pfname (previos filename) is not set, this is the first file found, so we don't delete that.
For every other file, if the filedate is the same as the previous filedate, then delete the previous filename.
The current filename/date is then recorded as the previous version.
Done!