Everyday at 1 am some zip folders will come in on one server(let us say A). I need to copy the files to another server(let us say B). After copying the files to server B, then after some time the copied files will moves to another destination. For the next day the previously copied files should not come with the files from today.
Now, the problem is, it is working fine in my Local PC .. It is not working in the server.
I don't have knowledge of batch file programming. Please let me know if there is a way to accomplish this.
My code:
#echo off
forfiles /s /p "\\10.35.0.44\Da\Ra\Folder" /m "*.zip" /d +"02/04/2019" /c "cmd /c move #path \\10.35.0.44\Da\sai"
pause
It is the throwing following error:
Error:UNC paths <\\machine\share> are not supported
Because of this I've used pushD()popD. The error was not there but the files are not being copied.
Related
I usually program in Java, but for reasons I won't specify I am trying my hand at solving a problem utilizing batch scripting. I have a couple directories that hold sub directories of days. For example I may have a folder called January and inside that folder are folders for each day (1, 2, 3, etc.) and inside those folders are the text files that I need to access. However, this data is constantly updated so I never have a set number of folders. I am wondering how I can access all the 'day' folders through a batch script?
I have been trying to iterate through the directories and figuring out what folders are inside the overall folder (ie: the month folder) by using the /d /r commands in a for loop. However this only gives me the files in the directory.
Some code snippets that I've tried are:
FOR /D /r %%P IN (..\JAN\Processed\) DO (
copy filex.txt ..\JAN\Processed\%%~nxP
/* run a correlation program using the file located in the directory specified with the copy command */
)
I've also tried to use
for /r "..\JAN\Processed\" %%P in (.) do (/*similar to code above*/)
When I run this code, it will access every directory and not just the ones that I want (because I have additional directories in the 'day' folders).
To test my code I have also used the ECHO command a lot, just to see if it's actually finding the directories I need by putting echo %%~nxP, but it doesn't seem to find the directories I want to access. It'll just return ECHO IS ON/OFF. Your help is much appreciated.
Get a for /d to get the subfolders (only depth=1) and a second nested for to get the files in that subfolder:
#echo off
for /d %%P in ("..\JAN\Processed\*") do (
for %%F in ("%%~fP\*") do echo %%~fF
)
I am trying to use XCopy to backup files from an old machine to a server folder. So I set up a batch file which copies each day the new files from the machine to the backup server using the following code, (just a part of the whole code):
set /p LastSuccessfulBackup=< C:/backup/LastSuccessfulBackup.txt
set datestr=%date:~-4,4%%date:~3,2%%date:~-10,2%
xcopy "\\machine\srcfolder" "\\server\destfold\"!datestr! /s /y /i /c /d:!LastSuccessfulBackup!
So the idea is to check whether there are changed files in the srcfolder using /d and copy them to a newly generated timestamp-folder if there are changed files. It's kind of an incremental backup.
My problem is now, that I would like to have the whole folder copied and not only the changed files. Otherwise I get day by day just a backup of some few files but never the whole src-folder content.
That means that I need kind of a check, whether there are changed files and if so, copy the whole srcfolder to the backup-destination timestamp-folder.
This batch script is supposed to map to PC and rename the files in that folder using the date & time stamp and copy them to another location on a different PC. It should then delete all the files in that source folder except for a file that called "LBBS.log". It all works fine except for the delete part. It isn't deleting anything in the folder and it is actually deleting the batch file itself. When I run it, it copies over fine but then it deletes itself. Can someone please tell me what I need to change for this to work. What am I missing? Its on a windows 7 environment. Thanks in advance.
net use x: \\MTLLBBS023\C$
set "stamp=%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%"
set "source=MTLLBBS023"
xcopy /S /E /I x:\logs E:\Data\Logs\MTLLBBS023\%source%-%stamp%.*
cd x:\Logs
for %%i in (*) do if not %%i == LBBS.log del %%i
net use x: /delete
The problem is that your script and target directories are located on separate drives.
When you cd to another directory, the command will fail if you try to move to another drive without using the /d option.
Instead of cd x:\logs, you should say cd /d x:\logs - this will change the drive and directory.
Alternatively, instead of the net use and net use delete commands, you can simply pushd \\MTLLBBS023\C$ to go to the network drive (this also automatically creates a temporary network drive) and then popd at the end of the script to leave the directory and remove the mapped drive. This way, you don't need to cd at all.
I want to use a batch file to synchronize 2 folders on Windows, and I'm using the xcopy command like this
xcopy /s /d <Original Folder> <Destination Folder>
It's working perfectly because I just want to overwrite only the modified ones, but I have 1 tiny problem, when I delete a file from the original folder, that file remains in the destination folder when i run the batch file, breaking the synching I want to achieve... I think this can't be done with xcopy, but I don't know which command I should use in case I delete a file in the original folder.
EDIT: This programs is for people who don't understant a lot of PC's so i don't want to install anything in their pc's. Just want a plain batch file to keep the 2 folders synchronized
SOLUTION: Thanks to Kuba Wyrostek I got to this command
robocopy <Original Folder> <Destination Folder> /e /purge
Thank you all for your time
xcopy has no such option. Your alternatives are:
rsync - http://rsync.samba.org
robocopy - http://technet.microsoft.com/en-us/library/cc733145.aspx
To refine your answer, you can simply do:
robocopy <Original Folder> <Destination Folder> /mir
/mir mirrors a directory tree (equivalent to /e plus /purge).
The XCOPY command for copying the updates from one folder to another is as follows:
C:\> XCOPY C:/SOURCE D:/DESTINATION /E /D /C /Y
The flags used in the above example have the following meanings:
/E causes all folders and sub-folders to be copied
/D causes a DATE comparison to be made, only copying items that are newer than the DESTINATION item. If the DESTINATION is older, or does not contain the file, then it will be copied.
/C tells XCOPY to continue if it encounters an error - typically errors occur with read-only files, or files that have protected permissions
/Y tells XCOPY to suppress prompting you for confirmation, which is necessary if you intend to create an automation script or scheduled task
I know this is old, but I have a question. Can this not be done with the correct xcopy commands.
Folder 1: c:\workingFolder\"contents here"
Folder 2: d:\workingFolderSync\"contents here"
Batch file below
xcopy "c:\workingFolder\*.*" "d:\workingFolderSync\" /i /s /d /y
xcopy "d:\workingFolderSync\*.*" "c:\workingFolder\" /i /s /d /y
pause
( I usually remove the pause after i test and know the batch works. )
But this will only copy contents from the folder to the other if its newer.
Then it looks in the destination folder and copies contents from destination back to source if it is newer. This reproduces what syncing does, except for the in real time monitoring of modified files. Creating a proper task schedule can actually implement running this command at specific times to almost perfectly reproduce syncing. You can also run this before modification on a machine and after modification on a machine. Even doing the same thing on a second machine if your using a network drive to sync.
Your method will have two main problems:
Files deleted from one folder will be reinstated by the copy left in the other folder the next time you run your xcopy
Renaming files can cause chaotic results.
Good day. I am trying to write my first batch file. I finally got it do to the most basic of tasks, and have read much here that helped including tutorials. I have not yet found an article to help me put what I'm looking for together. My search-fu is weak.
I want my batch file to copy all files every 30 minutes. However, if it comes to a file that has been updated, I want it to copy but not replace the existing in the destination. I'm assuming it would just add the (1) or 'copy' to the file name. I'm not sure if I used the proper switches or need to change them based on what I'm looking for. I think they're good, but I digress.
Here is the batch that I created without the logic:
REM Backup files 30 minutes
#echo off
:start
XCOPY "C:\source directory" "C:\target directory" /d /c /s /r /y /i
TIMEOUT /t 1800 /nobreak
goto start
Normal behaviour is to overwrite the file, or to fail (depending on the action). The (1) or (copy) postfixes are special features of Windows Explorer (the shell). If you want similar behaviour in your script, you'll have to implement it yourself.
The easiest thing (also to prevent a lot of (1), (2) .. (N) files, is to create separate folders. You can create a folder with a timestamp and copy all modified files to it.
To detect which files are modified, you might use the Archive flag of the files. When a file is modified, its Archive flag is set. XCopy has the possibility to copy only those files which have the flag set. Actually the main purpose of this flag is to determine modified files (or at least, files you want to archive).
So my suggestion:
Create a folder with a timestamp in the name. You may want to use the answer to this question for that.
Use XCopy with the /M parameter. /M copies only files with the Archive attribute, and clears the attribute afterwards.
Try to delete the directory using rd or rmdir, but without the /S parameter. It will fail if it contains files, but this way you will prevent a lot of empty directories.
Before implementing this, make sure that the process which modifies the files, also sets the Archive attribute again. It should do that automatically, but it can't hurt to test this.
-edit- Per request an example:
REM Backup files 30 minutes
#echo off
:start
REM generate a timestamp value that can be used as a file name.
setlocal TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
REM Not needed, I think: Creating the directory yourself.
REM md "C:\target directory\%TIMESTAMP%"
REM /S for recursive. /I should create target directory? /M for backup mode.
XCOPY "C:\source directory" "C:\target directory\%TIMESTAMP%" /M /S /I
REM Not needed, I think: Removing the directory (if it's empty).
REM rd "C:\target directory\%TIMESTAMP%"
TIMEOUT /t 1800 /nobreak
goto start
I think /I already solves the other issues. It assumes that target is a directory if it doesn't exist. That makes me assume that it will in fact create that directory if it doesn't exist, and probably only if there is anything to copy. If that is right, you also won't have to remove the directory if it's empty.
Considering cleanup: Remember that this method (using /M) only copies files that have changed. If you cleanup old directories, you should make sure to copy the entire folder first. The timestamped folders will contain only the modified files (incremental backup), so if you clean up old ones, your backup/copy won't be complete anymore!