My goal is to have a .bat that I can use to automatically archive folders via copy, rename the original folder and the files within with the current date and then clean out old production files.
Copy target folders to the "Completed" directory
Update date on the source folders using the format Previous Name_Today's Date (MMDDYY)
Update the date of the INDD files within the source folder using the format Current Name_Today's Date (MMDDYY)
"Clean" the source folder by deleting all .VPS and .PDFs files within the source folder
I am a novice but I have pieced together this code from research and sources:
#echo off
setlocal enabledelayedexpansion
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
xcopy /s /e /q /y "G:\...\Annual_*" "G:\...\_DONE\"
xcopy /s /e /q /y "G:\...\Life_*" "G:\...\_DONE\"
MOVE "G:\...\Annual_*" "G:\...\Annual_today"
MOVE "G:\...\Life_today" "G:\...\Life_today"
FOR /M *.indd /C "cmd /c rename #file \"#fname - today.indd\""
del /s "G:\...\Annual_today" *.pdf
del /s "G:\...\Annual_today" *.vps
del /s "G:\...\Life_today" *.pdf
del /s "G:\...\Life_today" *.vps
"G:\...\New_Job.bat" > output.txt
My end goal is to be able to change the directory path for the source and archive folders so I can reuse this script for different clients files.
Current Issues
As it stands the script doesn't copy and create an archive folder files and just, deletes all VPS and PDF files from all directories, not just the target folder.
I'm not sure if I'm performing the date check correctly to then use it as a variable to rename future folders and files.
I don't know if FOR /M *.indd /C "cmd /c rename #file \"#fname - today.indd\"" is correct to rename the files. The names are a PO number (6digits) then a title, underscore and then the date.
123456_Life_Kit_020819 for example.
Help would be greatly appreciated!
Related
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.
I have a temp folder where files get loaded into and then systematically purged out. Job to clear out files failed and now I have a number of directories/files that need to be manually deleted. I have deletedirs.txt that contains the names of all the folders I need deleted. I have generated the following to delete the files and folders however the path is not always the same:
FOR /F %%i IN (C:\dirlist.txt) DO echo y| del "C:\Temp Files\*" & rmdir /s /q "C:\Temp Files\" %%i
The issue is as the temp files get loaded in they form their own subfolder(s) so I have the following structure:
C:\Temp Files\101\folder1
C:\Temp Files\101\folder2
C:\Temp Files\103\folder4
C:\Temp Files\455\folder3
deletedirs.txt contains the folder names (folder1, folder2, etc.)
My script is failing because its searching for "C:\Temp Files\folder1"
How can I get the script to find 'folder1' and remove it regardless of subfolder number (101, 103, etc.)?
#ECHO OFF
SETLOCAL
FOR /d %%a IN ("c:\temp\*") DO (
FOR /f "delims=" %%s IN (q48890405.txt) DO (
IF EXIST "%%a\%%s\." ECHO RD /S /Q "%%a\%%s"
)
)
GOTO :EOF
You would need to change the directoryname to suit your circumstances.
I used a file named q48890405.txt containing some dummy data for my testing.
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.
This should assign the subdirectory names to %%a in turn, then for each subdirectory found, read the names from the file into %%s. Then see whether a directory named %%a\%%s exists, and delete it if it does exist.
Note that your del "C:\Temp Files\*" will simply delete all of the files in "C:\Temp Files" over and over again. It really only needs to be done once (if that's what you had intended to do).
Removing the directory disposes of all of the files it contained, provided none are protected in some way (read-only or by UAC).
I want a .bat file that will copy all files and folders from one folder to another folder automatically. For example:
robocopy "c:\source" "D:\target" /e /MON:1 /xc /xn /xo
However I need it so that once a file has been copied, that file will not be copied again, even if the copy has been moved to a different directory. Is there any way to do this? Is it possible for robocopy to create a log and check against that before copying the file?
If robocopy can't do it, what can?
Use additionally the option /M on your robocopy (SS64 article) command line as suggested by Stephan because this option results in copying only files with archive attribute set in source folder tree and removing archive attribute by robocopy after successful copying the file.
%SystemRoot%\System32\robocopy.exe "C:\source" "D:\target" /M /E /MON:1 /XC /XN /XO
The archive attribute is automatically set again on file modification.
You could perhaps also use xcopy (SS64 article):
%SystemRoot%\System32\xcopy.exe "C:\source" "D:\target\" /M /E /C /I /Q /G /H /R /K /Y >nul
Important for your task is again option /M for copying only files with archive attribute set in source folder tree and clearing the archive attribute after copying the file.
Note: /I works without user prompt only with target folder path ending with a backslash.
Run in a command prompt window robocopy /? respectively xcopy /? for details on the other options or read the Microsoft documentations for robocopy and xcopy.
Alright So, the easiest way to do this is to copy each
file and folder individually, to said folder.
This may not be what you are looking for, but
I hope it helps! Sadly there is no way to
copy l files folders with a single command.
I wish to copy a file with extension .dyn which is located in each subfolder of main folder(T15_finished). I wish to copy it into respective subfolder at other location(T15). I have created that location using xcopy command. Here, .dyn file is being copied successfully in respective subfolder in T15 folder(see below code). Now, I have a file which has extension as .dynain which is located in the same subfolder as .dyn. And .dynain file is also getting copied which i don't want.
Please see following code which i have created. Can anyone tell me whats wrong ?
#echo off
xcopy D:\Master\SPRINGBACK\CPW\T15_finished D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15 /t
xcopy /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
pause
Short file names. If you do a dir /x in the folder containing the .dynain file, you will see the 8.3 filename generated for the file, and it will have .dyn extension.
If you know the extensions of the conflicting files, you can use robocopy with /xf switch to indicate the files (*.dynain) to exclude, or you can generate a exclude file to use with xcopy /exclude:file (see xcopy /? for a explanation)
Or, you can generate the list of files to exclude
(for /f "tokens=" %%a in (
'dir /s /b "D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn" ^| findstr /v /i /e /l ".dyn"'
) do #echo(%%~nxa)>excludedFiles.txt
xcopy /exclude:excludedFiles.txt /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
Or (as posted by foxidrive), copy all and then delete the non needed files.
The short filename is being matched as well as the long filename. That is the reason.
A solution is to use another command to delete the files:
del /s "D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15\*.dynain"
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"
)