I'm trying to make a simple batch file to "sync" everything between a file on my onedrive, and a file on a network drive.
I want to be able to run the batch file, and anything new on the onedrive will be copied to the network drive, but not the other way around.
Here is my batch file:
#echo off
echo : XCOPY started
xcopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /E /D /C /Y /EXCLUDE:C:\exclude.txt
echo : complete
when run, this file seems to copy anywhere between 80-83 files. It is different each time I run it, despite me making no changes to either directory.
My understanding is that the /D flag for xcopy will ensure that it only copies files that are new, or have a newer modified date. But in this case, it seems to copy everything that isn't in my exclude.txt, regardless of date modified.
I think you have the two options:
for /f %%G in (c:\exclude.txt) do set "_excludes=%_excludes% %%~G"
robocopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /MT /E /MIR /XF %_excludes%
or
robocopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /MT /E /MIR
for /f %%G in (c:\exclude.txt) do del "%%~G"
You test with the /L option to simply log what it would have done.
Related
I use gallery-dl to download pics from Pixiv.com. All of the below are run from Windows' .bat files.
I manually create a folder with only {user[id]}.
Example: Pixiv - Temp\24517
I then copy gallery-dl.bat (which calls on gallery-dl.exe and the related gallery-dl.conf, elsewhere) and a number of assisting .jpg files into that folder.
Example: Pixiv-Temp\24517\gallery-dl.bat
I've done this for over 5,000 users.
Running gallery-dl.bat from within folder configured for the below, passing a variable of the folder name will download the pics as well as saving a *.sqlite3 file to the folder 24517.
gallery-dl.conf
"directory": ["{user[id]}"],
The actioning part of gallery-dl.bat (listed in whole, below).
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
The current .bat file in whole:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
move /y "..\Folder2.jpg" "..\Folder.jpg"
for %%I in (..) do set "FOLDER=%%~nxI"
for /f "tokens=1 delims= " %%a in ("%FOLDER%") do set tmpFOLDER=%%a
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
del "..\Folder3.jpg"
if not exist "GDB.sqlite3" move /Y "..\Folder4.jpg" "..\Folder.jpg"
if exist "GDB.sqlite3" del "..\Folder4.jpg" && del "..\Folder.jpg"
if exist "..\*.mp4" md "..\Reels" & move /y "..\*.mp4" "..\Reels\"
if exist "..\*.gif" md "..\GIFs" & move /y "..\*.gif" "..\GIFs\"
if exist "..\*.webp" md "..\WEBPs" & move /y "..\*.webp" "..\WEBPs\"
robocopy "..\..\%FOLDER%" "X:\11Web\gallery-dl\Pixiv - Temp\24517 -" *.* /e /move
TIMEOUT /T 1
popd
endlocal
I now plan to change gallery-dl.conf to {user[id]} - {user[name]}. This will result in a change to the destination folder being created now by gallery-dl. The original folder is left unchanged.
Example my original source folder: Pixiv-Temp\24517
Example gallery-dl new output folder: Pixiv-Temp\24517 - ケースワベ
gallery-dl.conf
"directory": ["{user[id]} - {user[name]}"],
gallery-dl.exe
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
Not knowing how a {user[name]} will come out in Explorer folder naming, I am left to still manually create an original source folder with only {user[id]}. I will continue doing this due to ease.
The above noted change will present problems I would like to overcome:
When running gallery-dl, the pics from Pixiv will be downloaded to an Explorer folder name of {user[id]} - {user[name]}, while the original manually-created folder still hosts the gallery-dl and *.sqlite3 file. I need these combined.
When re-running/updating gallery-dl against the 5,000+ other folders, I will be left with 5,000 of one {user[id]} and 5,000 of the other {user[id]} - {user[name]}.
Robocopy has shown the most promise in solving these. When run in the below .bat, gallery-dl runs and all files from {user[id]} folder are moved to {user[id]} - {user[name]} folder, leaving {user[id]} folder empty and I just run a script that deletes all empty folders. This has the added benefit that the .sqlite3 files are all moved.
MY ISSUE: I have not yet been successful with Robocopy without having to
explicitly name the path for the destination folder.
Example works:
robocopy "..\..\%FOLDER%" "X:\11Web\gallery-dl\Pixiv - Temp\24517 -" *.* /e /move
Example doesn't work:
robocopy "..\..\%FOLDER%" "..\..\24517 -" *.* /e /move
which after passing a second variable would look more like:
Example doesn't work:
robocopy "..\..\%FOLDER%" "..\..\%FOLDERDESTINATION%" *.* /e /move
nor
I have no idea how to pass a variable from a folder name that is not known in advance {user[id]} - {user[name]}, but must be searched for within the above root folder.
If there was a way from within the .bat file to
Declare a variable that matches the current folder {user[id]} (Already done in the below).
Example: 24517
and
And gallery-dl creates the new {user[id]} - {user[name]} and done its business, the .bat find and declare a variable that matches "{user[id]} -".
Example: 24517 - ケースワベ
As such, I will always know the first half of any variable:
{user[id]} -
or in this case
24517 -
Question is can the entire new folder name be searched for based on the known (Example 24517 -), a suitable secondary variable created equating to the new folder name, from only that?
And is then able to pass this variable to Robocopy in order to successfully move all desired files?
Example:
robocopy "..\..\%FOLDER%" "..\..\%FOLDERDESTINATION%" *.* /e /move
That would be just smashing!
Any and all ideas/assistance are appreciated. Thank you in advance!
Final result after applying successful answer from Stephan
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
move /y "..\Folder2.jpg" "..\Folder.jpg"
for %%I in (..) do set "FOLDER=%%~nxI"
for /f "tokens=1 delims= " %%a in ("%FOLDER%") do set tmpFOLDER=%%a
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
del "..\Folder3.jpg"
if not exist "GDB.sqlite3" move /Y "..\Folder4.jpg" "..\Folder.jpg"
if exist "GDB.sqlite3" del "..\Folder4.jpg" && del "..\Folder.jpg"
if exist "..\*.mp4" md "..\Reels" & move /y "..\*.mp4" "..\Reels\"
if exist "..\*.gif" md "..\GIFs" & move /y "..\*.gif" "..\GIFs\"
if exist "..\*.webp" md "..\WEBPs" & move /y "..\*.webp" "..\WEBPs\"
for /d %%a in ("..\..\%FOLDER% - *") do set "FOLDERDESTINATION=%%a"
rem ECHO %FOLDERDESTINATION%
robocopy "..\..\%FOLDER%" ".\%FOLDERDESTINATION%" *.* /e /move
TIMEOUT /T 1
popd
endlocal
When I understood your question right, this should help:
for /d %%a in ("..\..\%folder% - *") do set "folderdestination=%%a"
(Assumption: there is just one username per %folder% - else this will get only the last of them)
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!
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"
Currently running a backup with batch file using robocopy and using the /log command to write the output to a text file. The log file only contains the folder path currently and would like to remove everything except the file name that was copied over.
robocopy "C:\Junk" "H:\Junk"/s /mov /log:"c:\New Junk\Junk (%date:~-10,2%-%date:~-7,2%-%date:~-2,2%).txt" /NDL /NJH /NJS /NS /NC /NP
Eventually I'll have multiple text files that will need to be searched for the path "C:\Junk" to be removed from them. I've tried Findstr, but with horrible results. Any help would be appreciated
I was able to solve the issue by using a for loop with the /r switch. Here's the simple code
#ECHO OFF CD C:\Junk FOR /R %%G in (*.txt) DO ECHO %%~nG >> "H:\New Junk\Junk (%date:~-10,2%-%date:~-7,2%-%date:~-2,2%).txt" PAUSE
And then running the robocopy command without the