bat file debug "back up used files" - batch-file

Here is what I want to do: I want to write a "bat" file that will check all the files in a single partition to determine whether any file is revised/created today and if any, I would copy these file to a folder. So, if I run this bat everyday before I leave my office, I can backup all the files I used in a single folder. The bat file I have now copies the folder instead of file, and sometimes it doesn't work at all... Could you help me debug it? You might want to put it in a root directory such as C/D, and then change d:/test to whatever folder you plan to "test copy the targeted file.
Here is the code I have for now:
#echo off
set t=%date%
set t=%t:~0,10%
echo %t%
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=*" %%i in ('dir /b /a-d') do (
set d=%%~ti
set d=!d:~0,10!
echo !d!
if "!d!"=="%t%" (if not "%~nx0"=="%%i" copy "%%i" d:\test))
for /f "tokens=*" %%j in ('dir /b /ad') do (
set d=%%~tj
set d=!d:~0,10!
echo !d!
if "!d!"=="%t%" (echo d|xcopy /e /y "%%j" d:\test\%%j))

Can you just use robocopy? This line will copy all files in c:\source and its subfolders that have been modified in the last day, to d:\test.
robocopy c:\source d:\test *.* /s /maxage:1
Of course if you forget to run it one day, you'll miss any files touched that day. So if this is really for backups, the better approach is to use the Archive bit.
robocopy c:\source d:\test *.* /s /m
When a file is created or edited, Windows will clear the Archive bit. robocopy with the /m switch will only copy files with the Archive bit set (meaning only ones that have changed since the last time you ran your script), then sets the Archive bit.

Related

How can I create a batch script which selects the oldest file in a folder, renames it, moves it to another file and renames it again?

I am very new to programming/coding.
I'll try my best to explain my problem (sry for my english).
Im working on a Windows Server 2012 and I want to create a batch script which selects the oldest file in a folder, renames it, moves it to another folder and renames it again.
Another way to explain it:
I have a folder with 15 files in it 
The oldest file is called "orange" 
I want the script to rename the "orange" file
Now moving the file to another folder and finally rename it again
I got this so far from somewhere else in here but I dont know if it works or what exactly it means I just saved it so I have at least something to show you guys..
 
#echo off
cd c:\Test
for /F "delims=" %%a in ('dir /B /A:-D /O:D /T:W') do (
move "%%a" C:\Another\Location
goto continue
)
:continue
I think I have to include the following: ren [drive:][path]TargetMask but Im not sure at all...
I hope you can understand this. I already tried to look up my problem but just got more confused... Any help is greatly appreciated. Thank you
Windows 10 64-bit
How to move and rename the oldest (by last written time) file in a folder using cmd, pushd, for in do, set, move, and popd.
When you are happy the script will do what it says it will do make the following changes.
Change %userprofile%\desktop to C:\Another\Location
Change pushd "%userprofile%\desktop" to pushd c:\test
Change echo move to move
Rob Vanderwoude NT FOR
script:
#rem How to move and rename the oldest (by last written time) file in a folder using cmd, pushd, for in do, set, move, and popd.
#rem Windows 10 64-bit
#echo off
setlocal enableextensions
pushd "%userprofile%\desktop"
FOR /f "delims=" %%g IN ('DIR /b /a-d /o-d /tw') DO (
SET zold=%%g
SET zoldname=%%~ng
)
echo move /y %zold% %userprofile%\desktop\%zoldname%.bat
popd
exit /b
cmd:
cmd /eq
pushd "%userprofile%\desktop"
FOR /f "delims=" %g IN ('DIR /b /a-d /o-d /tw') DO (
SET zold=%g
SET zoldname=%~ng
)
echo move /y %zold% %userprofile%\desktop\%zoldname%.bat
popd
exit /b

batch file to Copy FOLDER with newest time stamp from network drive to local

I need to write a batch file to copy a folder, not file, from a network drive, that has been the most recently updated, to my local machine.
This is what I have, but it cannot find anything to copy because I want to copy a folder, not a file.
for /f "delims=" %%a in ('dir S:\Development\NightlyBuilds /B /A-D /O-D') do copy "%%a" U:\PWJ
Instead of copy, you can use xcopy. Use the /s flag to copy recursively. Make sure to terminate your PWJ folder with a \ to indicate that the destination is a folder. You can also use robocopy. It's a lot more robust. You can look up its parameters in robocopy /?.
You were using the wrong options for FOR and DIR commands, performing a copy for every file in date newest to oldest.
#ECHO OFF
(SET SrcDir=S:\Development\NightlyBuilds)
(SET DstDir=U:\PWJ)
IF NOT EXIST "%SrcDir%\" EXIT/B
IF NOT EXIST "U:\PWJ\" EXIT/B
IF /I NOT "%CD%"=="%SrcDir%" PushD "%SrcDir%"
FOR /F "DELIMS=" %%A IN ('DIR/B/AD-L/OD *') DO SET Latest="%%A"
ROBOCOPY %Latest% "%DstDir%\%Latest%" /E
EXIT/B

Recreate directory structure and copy newest file

I have 100+ sub-directories all under the same folder that I'm looking to copy the newest file to backup location with the directory structure intact.
\data\sub1\newest.file -> \backup\sub1\newest.file
\data\sub1\older.file1.ignore
\data\sub1\older.file2.ignore
\data\sub2\newest.file -> \backup\sub2\newest.file
\data\sub2\older.file1.ignore
etc....
Here's what I have so far, and i can't seem to piece it together. Any help would be greatly appreciated.
#echo off
set source="c:\data"
set dest="n:\backup"
if not exist %dest% md %dest%
cd /d %source%
for /d %%x in ("%source%"/*.*) do (
if not exist "%dest%\%%x" md "%dest%\%%x"
FOR /F %%I IN ('DIR *.* /A-D /B /O-D') DO COPY %%I "%DEST%\%%X" & #ECHO %%I COPIED TO "%DEST%\%%X"
)
I would try to do this with robocopy if I were you, because this will most likely be a more robust solution.
Windows' built-in xcopy program provides a flag to include empty directories.
C:\>xcopy "%source%" "%dest%" /E
But, it sounds like you may want to only copy newer/missing files. If that is the case, then #Marged has it right. You should use robocopy.
C:\>robocopy "%source%" "%dest%" /E
Check out robocopy /? for all the details and additional commands.
The metavariable %%x in your for statement is CaSe-SeNsItIvE so you must use %%x throughout the loop, but you are using %%X in the copy statement.
Since you only want to copy the first file, you should append &goto alabelotsideoftheforloop which terminates the for..x.. after the first file has been copied.

Create batch file to copy entire directory to new variable directory

Here's my problem. Playing a PC game (Arkham City) that can sometimes corrupt the save out of nowhere. My idea is to copy the save data to a new directory (as opposed to overwriting it) every time I start the game. In fact, this batch file will do everything. Copy files to new directory, and execute the game. I want it to copy all of the files to a variable directory every time I execute the batch into a new folder like Backup1, Backup2, etc.
There are two directories this games uses for save data - need them both:
C:\Users\Daddy\Documents\WB Games
C:\Users\Daddy\AppData\Local\Microsoft\XLive
Those directories and subdirectories when then copy to a location like this:
C:\Users\Daddy\Documents\Batman\GameSaveData\Backup1\ where the value after 1 is the variable. So literally every time I execute this batch, it dumps a new set of save data into a new Backup% folder at that directory.
The example below is static, but just overwrites to same directory. Unfortunately I wouldn't know if my game was corrupted until I go to play, but by then, the batch will have overwritten the good save with the corrupted save. This is why I want to always dump into a new backup folder.
xcopy "C:\Users\Daddy\Documents\WB Games" "C:\Users\Daddy\Documents\Batman\GameSaveData\WB Games" /D /E /C /R /I /K /Y /S
xcopy "C:\Users\Daddy\AppData\Local\Microsoft\XLive" "C:\Users\Daddy\Documents\Batman\GameSaveData\XLive" /D /E /C /R /I /K /Y /S
"C:\Program Files (x86)\WB Games\Batman Arkham City GOTY\Binaries\Win32\BmLauncher.exe"
I'd suggest you substring %date% and %time% to a yyyymmddhhmmss variable and use that in place of 1,2 etc.
Quite how you'd do that would depend on your date and time formats, but it's documented extensively on SO.
Using the yyyymmddhhmmss format will mean that the last directory in a dir list would be the last chronologically...
This should do the trick:
(will create a subfolder in "%Userprofile%\Documents\Batman\GameSaveData" named YYYY-MM-DD_HourMinuteSecond and copy the contents to it)
#ECHO OFF
FOR /F "SKIP=1 TOKENS=1-7" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF NOT "%%~F" == "" (
SET YYYY=%%F
SET MM=00%%D
SET DD=00%%A
SET HH=00%%B
SET Min=00%%C
SET Sec=00%%E
)
)
SET friendlyTime=%YYYY%-%MM:~-2%-%DD:~-2%_%HH:~-2%%Min:~-2%%Sec:~-2%
SETLOCAL ENABLEDELAYEDEXPANSION
IF NOT EXIST "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%" MD "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%"
ENDLOCAL
XCOPY "%Userprofile%\Documents\WB Games" "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%\WB Games" /D /E /C /R /I /K /Y /S
XCOPY "%LocalAppData%\Microsoft\XLive" "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%\XLive" /D /E /C /R /I /K /Y /S
Edit:
i haven't checked your XCOPY-Commandline, just copy and pasted it. :)

Batch file to copy a file based on date modified

Folder : C:\data\PRODDB\dir
In the above folder files get created daily in the morning .I want to create a batch script which checks/identifies that folder for the latest file based on date and copy to other location ( d:\test).
eg: file created as backup_110513.DMP 11/05/2013
We will use a backup software to schedule a backup job to backup the files in the folder(d:\test) and after that particular file gets backed up from d:\folder, need to create another script which empties the folder d:\test.
Thanks.
This will do the copy part of the files with modification date of today
forfiles /P "c:\data\PRODDB\dir" /M *.DMP /D +0 /C "cmd /c copy #path d:\test"
Try this. When the results you want are displayed remove the "echo.".
The goto is necessary so that we exit the loop immediately after copying the newest file.
#echo off
for /f "delims=" %%a in ('dir C:\data\PRODDB\dir /B /A-D /O-D') do echo.copy "%%a" d:\test & goto :Done
:Done
pause

Resources