adding dates to copied files - batch-file

I am trying to add the dates to copied files from one directory to another directory. This is how it should look like
Original file name: XEsalary.csv
Result file name: XEsalary-2013-02-15.csv
Here is my code:
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
cd %LOCAL%
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\*.csv"
:: assist with turning this into a for loop
ren %LOCAL%\archive\*.csv %LOCAL%\archive\*%PVM%.csv
echo. >>%LOG%\%PVM%.log
copying works just file. It is the renaming part which is not working. Any help please?
Thx in advance :-)

Using wildcards with RENAME is tricky. There is no good official documentation, but I have experimented and published rules as to how it works: See How does the Windows RENAME command interpret wildcards?
You can accomplish your rename with the following command, as long as none of your file names include dots (the last extension dot is OK).
ren "%LOCAL%\archive\*.csv" ????????????????????-%PVM%*
The number of ? must be greater than or equal to the longest name in the folder.
The result will be incorrect if a file name contains a dot. For example, part1.part2.csv would become part1-2013-02-15.part2.csv.
Another option is to use a FOR loop. You can safely append the date to any file using this technique.
for %%F in ("%LOCAL%\archive\*.csv") do ren "%%F" "%%~nF-%PVM%%%~xF"
BUT, both solutions have a problem if you rerun the process on a later date. New files will be added to the archive, but both new and old archive files will be renamed to the current date. That won't work.
Better to copy and rename the file in one step using a FOR loop as suggested in rojo's answer.
Or better yet, create a new archive folder with the date in the folder name, and then copy the files to the dated archive folder without renaming :-)
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
cd %LOCAL%
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
md "%LOCAL%\archive\%PMV%
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\%PVM%"
You might want to include the time in the folder name if the process could be run multiple times in the same day.

Windows doesn't let you rename with a wildcard. You have to name each file in a for loop. Might as well do the renaming as you're copying. Does this do what you intended?
#echo off
setlocal
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
pushd "%LOCAL%"
for %%I in (*.csv) do (
rem Uncomment the following line to log the copy to archive.
rem echo copy "%%I" -^> "%LOCAL%\archive\%%~nI-%PVM%.csv" >>%LOG%\%PVM%.log
copy "%%I" "%LOCAL%\archive\%%~nI-%PVM%.csv">NUL
)
echo. >>%LOG%\%PVM%.log
popd

Related

how to copy only modified files using LogFile.txt?

when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.

Script batch copy from subfolder to main folder filtering extension

I have this schema
SOURCE
FOLDER_A
---FOLDERA1
------file1.abc
------file2.abc
------file2.txt
---FOLDERB1
------file3.abc
------file4.abc
------file.txt
I want to create a batch script which copies in a new folder only
DESTINATION
FOLDER_A1
---file1.abc
---file2.abc
FOLDERB1
---file3.abc
---file4.abc
putting in the destination only the second level (FOLDER_A should be deleted) and filtering only files with .abc extension
I wrote this code
#echo off
set SOURCE_DIR=C:\Users\%username%\Desktop\SCRIPT\source2
set DEST_DIR=C:\Users\%username%\Desktop\SCRIPT\dest
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a In ('dir /ad/b %SOURCE_DIR% ') do (
set current_folder=%SOURCE_DIR%\%%a\
mkdir "dest\%%a"
for /r %SOURCE_DIR% %%f in (*.abc) do (
#copy "%%f" "dest\%%a"
)
pause
)
#pause
The problem is that in the destination I have the folder with the right name but inside of them everytime the 4 files file1.abc, file2.abc, file3.abc and file4.abc.
The goal is to have inside the first folder only file1.abc and file2.abc, and in the second folder file3.abc and file4.abc.
Where is the mistake?
Why are you using batchfiles and for-loops for this? Both xcopy and robocopy commands have exclusion features. Just type xcopy /? and robocopy /? for more information, and on the internet, you might find plenty of examples on how to do this.
Edit after first comment
It's indeed not that simple to work with the /Exclude switch, as you can see in following example:
C:\Temp_Folder\Folder_A>echo .txt>patterns.txt
// in this file, I mention that filenames, containing .txt, should not be copied
C:\Temp_Folder\Folder_A>xcopy /F /S C:\Temp_Folder\Folder_A\*.* C:\Temp_Folder\Destination\ /Exclude:C:\Temp_Folder\Folder_A\patterns.txt
// here I refer to the file, containing the patterns, not to copy
C:\Temp_Folder\Folder_A\FolderA1\file1.abc -> C:\Temp_Folder\Destination\FolderA1\file1.abc
C:\Temp_Folder\Folder_A\FolderA1\file2.abc -> C:\Temp_Folder\Destination\FolderA1\file2.abc
C:\Temp_Folder\Folder_A\FolderB1\file3.abc -> C:\Temp_Folder\Destination\FolderB1\file3.abc
C:\Temp_Folder\Folder_A\FolderB1\file4.abc -> C:\Temp_Folder\Destination\FolderB1\file4.abc
4 File(s) copied

Need Batch script to search and copy specific file from network drive

I am very new to batch scripting.
I need to write a batch script to search for a specific file(usually .zip or .7z extension) located on network drive directory(containing multiple folder and sub-folders with space in name) and copy the same to my local drive.
Also I need to copy a zip file containing "elf" keyword which will also be located in the same directory where my file is present.
For example:
Search file: abc.zip
Network drive:\abc.com\test
So basically I need to search for my file abc.zip in the network directory(including sub-folders) and if found copy the same to my local drive(say c:\Temp) and also I need to copy *elf* file to the same local directory.
Thanks in Advance.
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure paths
set "source=\abc.com\test"
set "target=c:\test"
rem Change drive/path to source of files
pushd "%source%"
rem Recurse folders searching adecuated files and when found, copy to target
for /f "tokens=*" %%f in ('dir /s /b "abc.*z*" ^| findstr /i /e /c:".zip" /c:".7z"') do (
copy /y "%%~ff" "%target%"
if exist "%%~dpf\*elf*.zip" copy /y "%%~dpf\*elf*.zip" "%target%"
)
rem Return to previous drive/path
popd
rem Clean
endlocal
This will search the source folder hierarchy for indicated filename and an aproximate match based on file extension (.*z* matchs .zip, .7z and more), filters file extensions to only accept the cases needed (.zip and .7z) and copy the required files to target folder.
Have you considered using the Extended Copy Utility (xcopy)? Syntax is as simple as
xcopy "<directoryToCopyFrom>" "<directoryToCopyTo>" /C /S /D /Y /I
This will work assuming you're wanting to write a Windows batch script.
Searching for the "elf" string will be a bit trickier though. You might consider doing that using Java, and then call the batch file from the Java program.
for /d /r "drive:\abc.com\test" %%A in (*) do (
if exist "%%~A\abc.zip" copy "%%~A\abc.zip" "C:\Temp"
if exist "%%~A\*elf*" copy "%%~A\*elf*" "C:\Temp"
)

Batch file to rename folders with wildcard or create a new folder and move contents?

I'm trying to tidy up a data folder and have written a batch file to take care of a lot of preliminary work (delete empty folders, delete junk files, etc), but I'm falling over when trying to deal with files within duplicate folders.
This is an example of the current situation:
w:\Data\Corporations\555\20130101\Concat_000001\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000002\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000003\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000004\555_20130101_data.zip
There should only be one Concat folder per YYYYMMDD folder, and should look like this:
w:\Data\Corporations\555\20130101\Concat\555_20130101_data.zip
There are hundreds of folders in w:\Data\Corporations to be processed, so I figure I need to first of all find any folder named Concat_*, make a folder named Concat within the same parent folder, and then move any zip from Concat_ to Concat.
I have tried various combinations of FOR /D in (Concat_*) with MD and MOVE commands, but with no luck so far. I've also tried calling a subroutine from the FOR statement that would jump back a level in the tree, create a folder named Concat, go back to Concat_* and move the .zip files, but again with no luck.
Any help would be greatly appreciated.
Cheers,
Pete
try this:
for /r "w:\Data\Corporations\555" %%a in (*.zip) do for %%b in ("%%~dpa.") do md "%%~dpbConcat" 2>nul & move /y "%%~fa" "%%~dpbConcat"
The following does what you asked. If you uncomment the last line, then empty config_* folders will be removed. Non-empty config_* folders will be preserved.
#echo off
for /f "delims=" %%F in ('dir /b /ad /s concat_*') do (
if not exist "%%~dpFconcat\" mkdir "%%~dpFconcat\"
if exist "%%F\*.zip" move /y "%%F\*.zip" "%%~dpFconcat\" >nul
REM uncomment line below if you want to remove empty concat_* folders
REM dir /b "%%F"|findstr "^" >nul || rd "%%F"
)

Windows batch move (or copy) PDF file by name to corresponding folder

I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.

Resources