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.
Related
I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.
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
So I have a bat file that runs just fine when I double click it. But setting it up to run with Task Scheduler and it will not run fully.
In this Bat file I use xcopy to copy two things:
Regular files.
Files modified in the last 30 days.
The "Regular files" part of this bat file runs fine with the task scheduler. However the "Files modified in the last 30 days" does not. (Again, running this bat file manually works.)
Contents of bat file:
set /p mydt=<tmpFile
set YYYY=%mydt:~6,4%
set MM=%mydt:~0,2%
set DD=%mydt:~3,2%
#echo %YYYY%
#echo %MM%
#echo %DD%
xcopy "\\TheServer\c$\TheFiles\*" C:\Dir\Files_younger_than\ /D:%MM%-%DD%-%YYYY% /Y /S
Xcopy "\\TheServer\c$\MoreFiles\*" C:\Dir\Morefiles\ /Y /S
Any thoughts why task scheduler is giving me issues here?
The answer is on the Actions Tab. Under Edit Action, put the bat file name in the program/script box. Then put the directory where the Bat file was located in the "Start in (optional)" area.
This should do the trick and the file should run properly after that.
Task scheduler runs under the System account by default and doesn't have access to network resources. Change it to run under your credentials and it will have the same permissions as you.
how can I organize my directories automatically?, I have a "downloads" folder, which currently contains lots of different info, for example, work related info, tv-shows, movies, etc, software, etc.
How can I automatically, maybe using some .bat execution, not sure, check for example the name of the files, or the type, and put them in the right subfolders?.
Thanks!.
You can use the move command to move files. You can also use wildcards in it.
So you could write a batch script that looks something like this:
cd C:\Users\You\Downloads
rem Excel sheets are work.
move *.xls Work
rem Reports are work.
move Report*.pdf Work\Reports
rem Pron and other viewing material ;)
move *.mp4 Private
You could run this script automatically by making it a scheduled job. Note that this script changes to the right directory first and then moves items to a relative subdirectory. This means that the directories Work, Work\Reports and Private must exist in the Downloads directory.
Of course you can expand the script to make it check and create directories as well, or you can specify different paths if you want to move the files out of the Downloads directory.
Basically, try to do it on the command line and then try to generalize those steps into your script.
This batch file will create a set of folders like .mkv .jpg .mp3 using the filetypes inside the folder, and move the files into the appropriate folders.
Launch it from your desktop and change the "c:\media" to the right folder name.
#echo off
cd /d "c:\media" && for %%a in (*) do md "%%~xa" 2>nul & move "%%a" "%%~xa" >nul
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.