understanding xcopy command - batch-file

I am modifying a batch script to move a directory from one place to another. In the batch code, there are these two lines. I don't understand the first parts
echo xcopy %SOURCE_FILE% %TARGET_FILE% /Y /Z /C /F 1>>"%logfile%" 2>&1
echo F|xcopy %SOURCE_FILE% %TARGET_FILE% /Y /Z /C /F 1>>"%logfile%" 2>&1
Do these lines of code execute the copy or just write out to the log file? I ask this because the code currently copies, however, there is no line of xcopy without the echo in front.
In line two what does F|xcopy mean?
Other then the F|xcopy on the second line, what is the difference between the two?

The first writes the command it is about to execute to a log file, but does nothing useful.
The second actually executes XCOPY but answers the question that gets asked with the letter "F" presumably when XCOPY asks "Do you mean Files or Directories?"

|is a pipe-symbol. It passes the output from the command before to the command after.
So the result of echo F (which is F) to the xcopy-command. So if xopy asks the user for input, it gets "F".
Simple example:
echo . | pause
Try it, pause waits for a keystroke, but echo . deliveres this keystroke.

just the report of what is to be done. better to use the /L switch on xcopy.
echo f| means to reply to a potential prompt with "f". The prompt is going to be "is the destination a directory or a file" the response is "file"
The first reports only, the second executes.

Related

Batch File Rd Error: The directory is not empty

I've been working on a code to delete the files from a Location inputted in .txt file.
#echo off
cd C:\Users\Troy\Desktop\Details
set /p loc=<Location.txt
rd /s /q "%loc%"
echo %loc%
pause
This code returns me the following output
The directory is not empty.
C:\Users\Troy\Downloads\TV Shows
Press any key to continue ...
Now the file Location.txt, when opened contains following
C:\Users\Troy\Downloads\TV Shows which is in accordance with the echo output I get in the second line (of the above output)
Also note that I have saved the batch file at C:\Users\Troy\Desktop
So there arises no reason for any interference due to the same location.
The weird part is when I run the following code from another batch file at the same location it runs perfectly fine and deletes all the files.
#echo off
set loc=C:\Users\Troy\Downloads\TV Shows
rd /s /q "%loc%"
echo %loc%
pause
So the only difference between the two codes is that the first one sets the variable location from a specific file, whereas the other one has a pre- inputted variable.
Also I have tried to delete files from the location using the following code
#echo off
cd C:\Users\Troy\Desktop\Details
set /p loc=<Location.txt
cd %loc%
del /s /q * >nul 2>&1
cd C:\Users\Troy\Desktop\Details
rd /s /q "%loc%"
echo %loc%
pause
In the above code, the delete command works perfectly fine and deletes all the files within. However folders and subfolders are all that are left, which means that rd command is not working
I've even tried the attrib -h thing, but that does not work either.
Also note that I've tried this with various permutations and combinations of rmdir /s /q too. But does not work.
Any help appreciated.
You could be suffering from some corruption in the file system. Try running chkdsk /f. You'll have to reboot in order to run it, but see if it finds something that it can correct, then see if your problem goes away.

Assistance modifying a batch file to delete the C: drive

Newbie question.
Would cd "C:del *.* delete all files within the C: drive?
I was also told if I add del . /F /Q it would bypass confirmation of the deletion, but I'm not 100% sure where to put it. Do I just add it onto the end like
cd "C:del *.* del . /F /Q?
This is a dangerous command. Be careful.
No, you probably want del /F /Q ..
The first part, del, is the command. This is telling it to delete something.
/F /Q are the options, denoted by the leading slash.
/F means to force it, so for example if a file isn't writable it will still delete it if able. It also skips some other checks.
/Q tells the program to be 'quiet', so less output will be generated than normal
.(dot) is the thing to delete, which is the place you currently are. If you are currently in C:\ (ie if the terminal displays C:\> at the beginning of every line), then it will be that that it deletes.
Note that I have only shown what you should type (del /F /Q .) and not what the entire line should look like when you're done (C:\> del /F /Q .). However, if this is going in a batch file then you want the first form only.
This is a destructive command and is provided merely to answer the query - don't try it at home or prank friends with it.
This will delete all files that are not locked, or opened for exclusive access by a program.
del c:\* /s /f /q /a
It will not delete every file in a Windows system drive and will not remove directories.

Timeout prompt won't stop showing

I'm attempting to use Batch for the first time, and I'm running into some trouble with the timeout command. I'm making a simple backup program to backup certain files to my flash drive, and this is the beginning.I'm trying to make it so that the prompt does not show how much of the countdown is left. This is what I have:
ECHO Deleting current backup location...
RD /s /q F:\CurrentBackup
#TIMEOUT /t 10
ECHO Setting up new backup...
MKDIR F:\CurrentBackup
MKDIR F:\CurrentBackup\Documents
MKDIR F:\CurrentBackup\Pictures
MKDIR F:\CurrentBackup\Desktop
MKDIR F:\CurrentBackup\Music
rem xcopy C:\Eric D:\
Can anyone help me with this seemingly simple problem?
you can tell a command, where to write it's output. If you don't, it writes it to screen
TIMEOUT /t 10 >nul
will write the output to a "Null-Device" (also known as "Nirwana")
by the way: # does not suppress the output of a command, but suppress the repetition of the commandline. It's a kind of "one-line-echo off"
Normally, you put
#echo off
as the first line of a script.
echo off will turn command repetition off, and the # does the same thing for this very line (as the echo off is not yet active for this line)

CMD "MOVE /Y" asks for confirmation

I'm still working on getting this: CMD for unable to move files due to string butchering to work.
My batch script looks like this right now,
FOR /F "delims=" %%T IN ('dir G:\ /B /A:D') DO (
CD "G:\%%T"
FOR /R %%D IN (*) DO (MOVE /Y "%%D" "G:\%%T")
)
PAUSE
and does as I intended, however as soon as it finds a duplicate file, it asks what it's supposed to do (overwrite: yes/no/all) for EACH file. Ordering to replace ALL only replaces one file.
Image: http://imgur.com/aKLsKs1
Why does it do that, and how do I fix it?
EDIT:
Turns out to be a windows bug of sorts. ROBOCOPY or XCOPY both work and their quiet switches work.
As you said, Robocopy does what you need and it smart about doing it.
http://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx
since windows 2000, the behaviour of the command is to prompt for confirmation regardless of the /y switch, unless the command is triggered from a script.
you can override this by setting the environment variable COPYCMD to /Y before running your move command. eg: SET COPYCMD=/Y && move /Y a b
this behaviour is documented at: https://ss64.com/nt/move.html
Funny bc.
/y : Suppresses prompting to confirm you want to overwrite an existing destination file.
/-y : Causes prompting to confirm you want to overwrite an existing destination file.
Have u tried a little /y ?
Strange thing...
move /? says (translated, because my help is german):
you have to confirm overwriting by default, in spite you call it from a batch file.
I just tried this very simple batchfile:
move test.txt test
test.txt is moved into folder test and the existing file is overwritten without confirmation.
When I give this command at the prompt (not batchfile), it asks for overwriting (as designed)
So all you have to do is removing /Y (strange, but fact)
(using Win7 - if this matters)

How to silently delete files with a bat file

del /s .jpg
deletes all .jpgs .. but the problem is: it shows, in cmd when executed =>
C:\blabla..\this.jpg is deleted..
I want to turn this off. Such that user will not know what is happening (i.e, what files are being deleted).
Turn echo off to suppress showing the command being run, and redirect output to null as #Sico suggested.
#echo off
del /s *.jpg >nul 2>&1
You should see nothing displayed when the bat file is run.
In jammykam's answer, he uses >nul 2>&1. What this does is redirect both standard output and standard error to the null device. However, hiding the standard error is not best practise and should only be done if necessary.
#echo off
del /s *.jpg 1>nul
In this example, 1>nul only hides the standard output, but standard error will still show. If del fails to delete some files, you will be informed.
Read more about redirection
Try putting this at the top of your batch script:
#echo off

Resources