Trying to learn batch script, what does this do: /letter? - windows

Here's the batch file:
#echo off
xcopy c:\testsource c:\testbackup /m /e /y
So I know that "xcopy c:\testsource c:\testbackup" copies the files in the testsource directory and pastes them in the testbackup directory, but what does the "/m /e /y" do? Can you explain what each individual part of this section of code does?
Also, would you mind telling me where I could look up explanations of syntax like this on my own in the future so I don't have to keep asking simple questions like this (a command for command prompt, maybe online database of commands and explanations, just something more efficient than google).

Basically, in batch you first enter the command to be excuted, and then supply the arguments. Each command can be seen as an own small programm.
you are calling the command xcopy and supply those five arguments to it.
A documentation can be found here: https://technet.microsoft.com/de-ch/library/cc771254.aspx?f=255&MSPPError=-2147217396
you can also type in help COMMAND, for example help xcopy, to see a full list of arguments which can be passed to the command

when in doubt, use the mighty /? behind most commands to get more information and extra switches. i.e: xcopy /? additionally, information on the /m /e and /y switches:
xcopy /M Copies only files with the archive attribute set,
turns off the archive attribute. xcopy /e Copies directories and subdirectories, including empty ones. and xcopy /y Suppresses prompting to confirm you want to overwrite an
existing destination file.
hope that helped!

Related

I want to copy a directory excluding one file

I have used the xcopy command with /EXCLUDE switch but for that i have to make another file that contains the list of files to be excluded.
Below is the xcopy command i am using...
xcopy /EXCLUDE:C:\AA\excludedfiles.txt C:\AA d:\Models\Broker\NB\MOTNB0056
/S /E
where excludedfiles.txt contains the name of file that i want to exclude.
C:\AA is source and d:\Models\Broker\NB\MOTNB0056 is destination.
However i don't want to make extra file(excludedfiles.txt) for it. Suggest a command that exclude a file by giving just its path.
XCOPY is deprecated. It has been replaced by ROBOCOPY.
Open a command prompt window an run robocopy /? for help on command ROBOCOPY. In comparison to help of XCOPY output on running xcopy /? it has the option /XF to exclude one or more files specified after this switch and it is even possible to use wildcards.
So the command you might use is:
%SystemRoot%\System32\robocopy.exe C:\AA D:\Models\Broker\NB\MOTNB0056 /E /XF C:\AA\FileToExclude.ext
Some additional notes:
/S means copying with subdirectories, but without empty directories.
/E means copying with subdirectories with including empty directories.
It does not make sense to specify both on the command line.
It is advisable to specify target directory with backslash at end. This makes it clear for ROBOCOPY as well as for XCOPY that the target string specifies a directory and not a file. This is important in case of just a single file is copied as you can read in answer on batch file asks for file or folder. Both commands create the directory tree to target directory if this is necessary on having target directory specified with \ at end.
See Microsoft's documentation on Windows Commands and SS64.com - A-Z index of the Windows CMD command line on searching for a command for a specific file operation from command line or batch file.

Xcopy complete operation successfully, but the copy does not occur

i'm having a weird problem about copying with Xcopy.
I'm using Windows Server 2008 R2. There is a Batch file (.CMD) - in fact, 2 Batches - which executes many operations. Among them there are 2 Xcopy.
The first Xcopy completes the copy successfully. Then some operations of batch are executed and...the second Xcopy is executed after some time. It shows a successfully status, like "300 files copied" but...the files are not copied !
THE strange behaviour: i put a pause command into the Batch and, after this second weird Xcopy, i run THE SAME command at the Prompt and..it works !!!
I cannot explain this, so i'm asking for help here. The full command is:
xcopy /s /e /h /r /i /y E: D:
As a matter of curiosity/information, the other Xcopy (which worked!) is:
xcopy /s /e /h /r /i /y E: D:
Or....the same command !!! Each one runs in a separated Batch file - the first Batch "calls" the second one.
E: is CD-ROM, so after the first Xcopy, the first Batch asks for another Disc and calls the second Batch, which (supposed) copies entire CD content to D: (HDD).
I'm sorry if the question is silly and i'm not realizing how simple is to correct the erratic behaviour of Xcopy. Thanks in advance...
Thanks very much Bali C !
But i found the problem: the correct syntax is
xcopy /s /e /h /r /i /y E:\ D:\
You can see that the backslashes made all the difference ! The files were being copied to another directory, in fact, the current working directory. With backslashes the copy is fine.
But i appreciate your suggestion...
To copy the entire contents try using
xcopy /e /h /r /i /y /t E:\*.* D:\
I left out the /s as it contradicts the /e switch, I doubt it will be the source of the problem but it's worth a shot. I used the \t switch to copy the directory structure of the source.
I have also used wildcards *.* to copy the contents, rather than just the drive letter, some things work at cmd prompt but not in batch, but try using this.
Another option would be to use robocopy.

How to use a batch command to copy all folders to a destination location

I have the following line in a bat file:
xcopy script_temp\* \\CHU-Computer-Science\CHU\scripts\ /S /E /H
however, this will only copy files, how can i also have it copy the folder?
The options you used copied empty folders for me on Vista. I don't know if there are differences in versions of Windows.
The /S and /E are mutually exclusive. /E copies subfolders, including empty ones. /S copies subfolders but ignores empty ones.
I thought that the last option specified wins, so the command you used should work. You could try eliminating the /S option and see if that helps on your system.
Or possibly I don't understand your question?

Batch or VB Script to copy logs from multi directories to one directory and rename to avoid conflicts

Hope you can help. I have been trying to resolve this for a week but not getting anywhere and can't quite piece together what I need! - My scripting skills are far from great so please forgive my naivety!
Ok, The Problem......
I have an IIS server that has multiple sites that all save their logs in a separate directory, I need to copy the logs from the last 24 hours to a local directory on my machine so I can analyse these in Log Parser Lizard (GUI Version) on a daily basis.
I can map a drive from the remote server to my local machine via a hardware VPN, so this makes things a bit easier. Using the forfiles command I can re-curse the directories to find the logs that are only a day old, and using either copy/xcopy/ or Robocopy I can set a command to copy. My problem is that the IIS logs all have the same name so my copy command just keeps overwriting the previous file, rather than creating a new file. I have tried using the %random% parameter for the file name, but this again creates one random file that is overwritten with the next file, keeping the same name instead of creating lots of randomly named files in one directory.
I know that Log Parser commands include recurse, which I have used successfully, however the format of the log is changed slightly and the GUI Lizard cannot read the data within, so this is not a solution.
My code as it stands at this time is shown below, with IP's changed for obvious reasons. Any help would be greatly appreciated!
#echo off
NET USE Q: /Delete /yes
NET USE Q: \255.255.255.255\D$\Logs
cd C:
RD /S /Q C:\Weblogs\Production
MD C:\Weblogs\Production
forfiles.exe /p Q:\ /s /m *.log /d 0 /c "cmd /c robocopy /S /XC /XN /XO #file C:\Weblogs\Production\%random%.log"
NET USE Q: /delete
exit
%RANDOM% does not work for you in this case because it does not get solved per each iteration but only once at the forfiles invocation.
You'll need either to use in FORFILES some unique identifier, maybe concatenating #RELPATH and #FNAME may work for you in case you have only one level deep recursion.
Or either replace FORFILES with a FOR loop. Inside the loop you may have more freedom to calculate a unique ID, maybe a simple counter might work for you.
Edit: see this simple code sample, to get you started
#ECHO off
SETLOCAL enabledelayedexpansion
SET destdir=C:\LOGS
SET count=%RANDOM%
FOR /R %%A IN (*.log) DO (
SET /A count += 1
#ECHO COPY %%A !destdir!\%%~nA.!count!.log
)

DOS command to replace all instances of <filename>.config

i have an edited version of a config file specific for my machine.
i have the same config file in multiple different directories in my development folder.
i want to, in a single bat file, replace all instances of this file with my edited one.
So in pusedo code:
Take C:\edited.config and copy to C:\Projects\ /s wherever original.config is found
i want the final file to have the name of original.config, not edited.config
so i am guessing i need some combination of a FOR, a rename and copy or something like that
is this easier to do in Powershell?
can anybody help?
Thanks
I blogged about this a little bit ago at http://jamesewelch.com/2008/05/01/how-to-write-a-dos-batch-file-to-loop-through-files/
I think your solution will look something similar to (below is untested but used to show general idea)
for /f %%a IN ('dir /b *.config') do copy c:\master.config %%a
There's probably a switch there on the copy to suppress file overwrite warnings, but I don't remember what the switch is. This will copy your master.config and overwrite your local file (variable of %%a).
I'm amazed what DOS batch file experts make work. Since I'm not one of them, I take an approach that's pragmatic for me. It might work for you as well.
Get a list of destination folders
C:
Cd\
Dir original.config /s > original.bat
Edit original.bat in your favorite text editor (I like Notepad++)
Search for "original.config" and replace with "" (empty string)
Insert the text "Xcopy C:\edited.config " at the front of each line
Proof-read the result to be sure it's what you want. If you're not sure put an "Echo " in front of each line for a dry run.
Run the batch file.
#echo off
C:
cd \Projects
FOR /F "tokens=*" %%G IN ('DIR /B /S original.config') DO xcopy /y c:\edited.config %%G

Resources