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
Related
I have an Excel template that I will be needing to copy into hundreds of folders. I want to create a batch script that will do this for me.
Here's the catch: I need this to work based on whatever folder the batch file is in. I need a script that can be run from whatever the directory the batch file is in without specifying a specific directory or destination. The script will detect that excel file and then copy it into every subfolder in that directory.
Everything I've found so far requires you to specify the directory and destination. I would like something that can be moved to any computer and work based on the setup shown below:
Is this possible? Any help would be much appreciated! Thank you.
Make the current working directory the one where the .bat file script exists using PUSHD. Go back to the previous directory with POPD.
#ECHO OFF
PUSHD %~dp0
SET "TEMPLATE_FILE=Property Inspection Form TEMPLATE.xlsm"
IF NOT EXIST "%TEMPLATE_FILE%" (ECHO ERROR: No template file found in "%CD%" & GOTO TheEnd)
FOR /F "delims=" %%d IN ('DIR /B /A:D') DO (
COPY /Y "%TEMPLATE_FILE%" "%%~d"
)
:TheEnd
POPD
EXIT /B
If you want to copy any and all .xlsm files to the subdirectories, you could do something like this. There really should be error checking after the COPY command to see that it worked.
#ECHO OFF
PUSHD %~dp0
FOR /F "delims=" %%d IN ('DIR /B /A:D') DO (
COPY /Y "*.xlsm" "%%~d"
)
:TheEnd
POPD
EXIT /B
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
I have made a bat script as follows
cd "D:\ACT\ACTBKUP"
del /Q *.*
FORFILES /P E:\ACT_Backups /M *.zip /D +1 /C "cmd /c del #D:\ACT\ACTBKUP"
The script is supposed to delete everything in "D:\ACT\ACTBKUP" and then move the newest zip folder to that same folder from E:\ACT_Backups. When I run this script from windows server 2012 it just disappears.
thanks
In order to switch to a directory that is located on a different drive, you need to use cd /d instead of just cd. If you do not do this, the directory will not change.
When you run a script by double-clicking on it, batch considers the current directory to be the directory where the script is currently located. Since you are not using the /d option with cd, you are running del /Q *.* on the directory where the script is located.
To fix this, you have two options:
cd /d "D:\ACT\ACTBKUP"
del /Q *.*
or
del /Q "D:\ACT\ACTBKUP"
There is no option in forfiles to get just the most recent file; /D +1 will return all files with a last-modified date of today or later. In order to get the most recent file and nothing else, you will need a for /f loop:
rem /b returns just the file name and /o:d sorts the files by date
rem Since newest_file gets set for each .zip file in the directory,
rem the last file set will be the newest
for /f "delims=" %%A in ('dir /b /o:d E:\ACT_Backups\*.zip') do set newest_file=%%A
copy %newest_file% D:\ACT\ACTBKUP
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. :)
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.