I'm trying to make a batch file through Notepad to essentially automate a process of moving files from drive to drive.
My aim is to move files from my H drive to my A drive, H:\Arco\examplefile.csv to \A:\DSE\Open_Access_Data\ARCo.
I also want to automate this job to run every 30 minutes if possible. But if I need to do it by clicking it then so be it. So far, all I've managed to do is copy files over to my desktop. I can't seem to get it to go between my directories.
COPY H:\dehpc14_Disk_Quota_Report.csv %userprofile%\Desktop
This should work:
:LOOP
copy H:\Arco\examplefile.csv A:\DSE\Open_Access_Data\ARCo /y
timeout /t 1800
goto :LOOP
That will copy your files every 30 seconds and overwrite any existing files.
Note: The timeout command is only availble in Vista and above, if you need to use this on XP let me know.
To launch the batch file on system startup you can either put it in the startup folder of the user or use the registry.
The startup folder for the current user is
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
For all users
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
Or you can use the registry which I personally prefer. Create a string value with the path to your batch file in
Current user
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
Computer users
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Both registry options will require admin rights though.
To do either startup or registry in batch respectively
copy %0 "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" /y
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "MyBatchFile" /d "%0" /f
Which will either copy itself to startup or add itself to the registry each time it runs. So you can either do it manually or have it do this itself (above commands) from the first time you run it.
The %0 is the batch files own path, if you want to use the commands from somewhere else, or just from cmd then type in the full path of the batch file instead.
Use a command like cron in Unix to setup a timed interval to run programs like your automated copy. See Stack Overflow question What is the Windows version of cron? for a cron-like version for Windows.
To copy from one drive to another use
copy filepath1 filepath2
where filepath1 is your H:\path-to-file and filepath2 is your A:\path-to-file.
Related
If I have call C:\Users\user\Desktop\Main\folder\edit.bat
What is the right way to set this up this:
I want to eliminate C:\Users\user\Desktop, and make my batch universal so that I can plug in to any computer, run it from a usb, or external or simply add my batch to the desktop of anyone's computer. And run it without having to edit source or target again.
If I want it to run my edit.bat from any computer do I set it up like this
call %SystemRoot%\Main\folder\edit.bat
or
call %UserProfile%\Desktop\Main\folder\edit.bat
or
call %UserProfile%\Main\folder\edit.bat
If I understand Mofi right I can change this
call "C:\Users\user\Desktop\Main\folder\edit.bat" Original
to this
call "C:\users\%username%\Desktop\Main\folder\edit.bat" Works
and to this
call "%UserProfile%\Desktop\Main\folder\edit.bat" Works
Can anyone tell me if this is the correct
call "%~dp0\edit.bat"
If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\ or %userprofile%\documents\.
#echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist checks for the directory. mkdir creates the destination directory. Pretty obvious but none-the-less.
%~dp0 defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile% environment variable is by default equal to the directory C:\users\%username%\, and %computername% expands to the computer's name.
xcopy takes the source directory and copies to our destination we created prior. /Y forces the copy and does not prompt to overwrite files.
* is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\* will not resolve. For something like that you would need to use for /D.
Lastly I always like to check for errors, and see if it was successful. If not we pause to make sure we see the error, or we put in a timeout /t seconds to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.
If you want to open a file in your portable drive you can do something like this
%~d0\folder\folder\file
I am trying to copy a file and folder from on my flash drive to the desktop of any computer I plug it into. I am somewhat familiar with the command line and batch scripts but have not used it for doing file manipulation in this way.
I originally wrote a script to test copy it from my flash drive to my desktop, however, the issue is on a new computer I won't always have the same drive letter assigned.
The code I used to test copy everything with known drive letters where the script starts in the folder on my flash drive where what I want to copy is:
copy aFile.file C:\Users\*user*\Desktop
where user is my user folder.
The main issue I am running into right now is copying things from one drive to another where it needs to go into a user profile
cd has the /D switch which specifically specifies to switch drives.
so
cd /D %userprofile%\Desktop
goes to the active user's Desktop... right where I want it.
whereas
cd %userprofile%\Desktop
does nothing because users are on a separate drive.
Is there something similar to /D for copy? The main issue is that drives letters change and user names change.
User name changes are easy to deal with because I can use %userprofile\Desktop.
If we assumed that copy made use of the same switches as cd (it doesn't) then what I basically want is:
copy /D aFile.file %userprofile%\Desktop
I am sure I am missing something relatively simple but I didn't see any switches that seemed to be what I need.
I am also open to using robocopy if that would be easier, but attributes (timestamp, owner, etc) don't matter to me so I didn't think it would benefit me immediately.
batch scripts may not be compatible with cd
try to make a script that asks two inputs.
#echo off
set /p var1="this is the file that will be moved"
set /p var2="this is the destination"
xcopy %var1% %var2%
echo copied!
pause >nul
this will ask for two inputs,first input asks for the file,second one is the file copy to
I have an archive.pst file on my C: drive that I use in outlook to backup my email. But my C: is not backed up each night. So I'd like to copy the .pst file to my network drive so it will consistently be backed up. I do not want outlook to open the .pst file directly from the network drive for a variety of reasons.
Therefore I am trying to create a scheduled task that will copy my .pst file to a network location each day. The batch file below works perfectly if double-clicked. If I try to run the scheduled task, only the log file is created. Outlook doesn't close and the .pst file is not copied. I've tried running with the highest privileges but that doesn't seem to help. Any ideas would be appreciated.
cscript.exe close_outlook.vbs
::This is my VBS Script
::Set Outlook = CreateObject("Outlook.Application")
::Outlook.Quit
ping localhost > nul
set idrive="\\myserver\drive\\Outlook Files\"
set current="C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time% >> %idrive%\Log.txt
copy %current%\archive.pst %idrive%\archive.pst /y
echo End Time of Copy: %time% >> %idrive%\Log.txt
move %idrive%\Log.txt %idrive%\BackupLogs\Log.txt
ren %idrive%\BackupLogs\Log.txt %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
cscript.exe open_outlook.vbs
::This is my VBS Script
::set shell = createobject("wscript.shell")
::shell.run "outlook.exe"
EXIT
In reviewing the previous responses, I have shortened the batch file to only the code below. This works when double-clicking, but not when scheduling a task. I've also tried the same task moving the .vbs script to a network drive. Same outcome.
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\open_outlook.vbs"
1. Specify all files in a batch file executed as scheduled task with full path.
Double clicking on a batch file results usually in running the batch file with currently working directory being the directory of the batch file. But when running a batch file as scheduled task, the system32 directory of Windows is the current working directory.
Is close_outlook.vbs and open_outlook.vbs in system32 directory of Windows?
I don't think so. Replace in batch code below Path to\Script File twice by the right path.
2. Assign string values with space to environment variables right.
variable=value is the parameter for command set. With
set idrive="\\myserver\drive\\Outlook Files\"
you assign to variable idrive the value "\\myserver\drive\\Outlook Files\" with the double quotes included. This results on expansion of
echo End Time of Copy: %time% >> %idrive%\Log.txt
in the command line
echo End Time of Copy: 19:21:53 >> 1>"\\myserver\drive\\Outlook Files\"\Log.txt
and this is not right, isn't it.
Correct is:
set "idrive=\\myserver\drive\Outlook Files"
I removed also second backslash after drive and the backslash at end of folder path.
As the environment variable contains now the path with space(s) without double quotes, the double quotes must be added where the value of the environment variable is used concatenated with a file name, see batch code below.
There is one more reason why using "variable=value". A not visible trailing space at end of the line with command set in batch file is also appended to value of the environment variable if double quotes are not used or used wrong. Read this answer for details about correct assignment of string values to environment variables.
3. Define the wait loop using command ping better.
The command
ping localhost > nul
produces a wait. But it is better to use something like
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
as now the wait is determined with exactly 3 seconds.
4. Do not insert a space left of redirect operators > or >> for stdout.
Why this should not be done was explained by me here in detail.
5. Avoid environment variables not defined in batch file itself or in system account.
Your batch file uses only environment variables defined in batch file itself. So this advice is here not really needed.
However, many batch file working fine on double click but not on running as scheduled task fail because the batch file depends on environment variables like PATH or others which are related to current user account. It is safe to use environment variables which exist for all accounts like SystemRoot.
Reworked batch code
Here is your batch file with the appropriate changes whereby the paths of the two *.vbs files must be set correct by you before the batch file (hopefully) works as scheduled task.
%SystemRoot%\System32\cscript.exe "Path to\Script File\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
set "idrive=\\myserver\drive\Outlook Files"
set "current=C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time%>>"%idrive%\Log.txt"
copy /B /Y /Z "%current%\archive.pst" "%idrive%\archive.pst"
echo End Time of Copy: %time%>>"%idrive%\Log.txt"
move "%idrive%\Log.txt" "%idrive%\BackupLogs\Log.txt"
ren "%idrive%\BackupLogs\Log.txt" %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
%SystemRoot%\System32\cscript.exe "Path to\Script File\open_outlook.vbs"
set "idrive="
set "current="
I need to automate a batch script to copy project data from the server and replace into a project folder on each of the 30+ users's "My Documents" folder.
The users will be in a network, and I want the batch file to automatically run at login.
How do I modify my code from:
#echo off
COPY \\servername\Project\Data\*.* C:\TEMP\"FileCopy Test"\*.*
Echo Done.
pause
To copy into every user's My Documents directory? My attempt below failed:
#echo off
COPY \\servername\Project\Data\*.* C:\Documents and Settings\user.name\My Documents\FileCopy Test\*.*
Echo Done.
pause
Please help:
You can use the Enviroment variables of Windows:
#echo off
COPY \\servername\Project\Data\*.* "%USERPROFILE%\My Documents\FileCopy Test\"
Echo Done.
pause
PS: And remember to use doublequotes with several space names in folders.
Is it possible to create a batch file to copy a folder to another location everytime I login, or when the folder is updated?
It could be written in VB or Java as well if not an easy solution.
Any ideas?
Two approaches:
When you login: you can to create a copy_my_files.bat file into your All Programs > Startup folder with this content (its a plain text document):
xcopy c:\folder\*.* d:\another_folder\.
Use xcopy c:\folder\*.* d:\another_folder\. /Y to overwrite the file without any prompt.
Everytime a folder changes: if you can to use C#, you can to create a program using FileSystemWatcher
#echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
Open Notepad.
Type the following lines into it (obviously replace the folders with your ones)
#echo off
rem you could also remove the line above, because it might help you to see what happens
rem /i option is needed to avoid the batch file asking you whether destination folder is a file or a folder
rem /e option is needed to copy also all folders and subfolders
xcopy "c:\New Folder" "c:\Copy of New Folder" /i /e
Save the file as backup.bat (not .txt)
Double click on the file to run it. It will backup the folder and all its contents files/subfolders.
Now if you want the batch file to be run everytime you login in Windows, you should place it in Windows Startup menu. You find it under: Start > All Program > Startup
To place the batch file in there either drag it into the Startup menu or RIGH click on the Windows START button and select Explore, go in Programs > Startup, and copy the batch file into there.
To run the batch file everytime the folder is updated you need an application, it can not be done with just a batch file.
It's easy to copy a folder in a batch file.
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And you can add that batch file to your Windows login script pretty easily (assuming you have admin rights on the machine). Just go to the "User Manager" control panel, choose properties for your user, choose profile and set a logon script.
How you get to the user manager control panel depends on which version of Windows you run. But right clicking on My Computer and choosing manage and then choosing Local users and groups works for most versions.
The only sticky bit is "when the folder is updated". This sounds like a folder watcher, which you can't do in a batch file, but you can do pretty easily with .NET.
Batch file to copy folder is easy.
xcopy /Y C:\Source\*.* C:\NewFolder
Save the above as a batch file, and get Windows to run it on start up.
To do the same thing when folder is updated is trickier, you'll need a program that monitors the folder every x time and check for changes. You can write the program in VB/Java/whatever then schedule it to run every 30mins.
robocopy yourfolder yourdestination /MON:0
should do it, although you may need some more options. The switch at the end will re-run robocopy if more than 0 changes are seen.
#echo off
cls
echo press any key to continue backup !
pause
xcopy c:\users\file*.* e:\backup*.* /s /e
echo backup complete
pause
file = name of file your wanting to copy
backup = where u want the file to be moved to
Hope this helps
#echo off
xcopy ...
Replace ... with the appropriate xcopy arguments to copy what you want copied.