CMD "MOVE /Y" asks for confirmation - batch-file

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)

Related

How to extract part of a string using only variables in a batch file?

Maybe I'm trying to oversimplify things. Having trouble removing part of a variable to be used as a path name. Goal is to transfer everything from folders (and sub) on multiple windows profiles to a single profile.
I can copy all the content, but cannot tell it where to place it. Also happen to be trying to use robocopy instead for the first time. I want to make a folders on the desktop that are named the username of every user on the device, then copy all content.
for /d %%a in (C:\Users\*) do (
set var=%%a
var=${var#*s/ }
mkdir "%USERPROFILE%\Desktop\Backup\%var%\folderx"
xcopy "%%a\Desktop\folderx" "%USERPROFILE%\Desktop\Backup\%var%\folderx" /E /Y
)
Notes:
Not sure the best way to remove C:\Users\ so I leave just the username as a variable (it assumes var is a command)
I'm trying to do it without having to write a string to file and read it back
This assumes the Backup folder is already created
Still not sure how to strip it correctly. Just found it more efficient to not include the drive in the first place.
List item dir bare command to get name
changed C: to user variable in case installed on a different letter
FOR /f "tokens=*" %%a in ('dir /b /a:d-s-l "%SystemDrive%\Users"') DO (
mkdir "%USERPROFILE%\Desktop\Backup\%%a\folderx"
mkdir "%USERPROFILE%\Desktop\Backup\%%\foldery"
xcopy "%SystemDrive%\Users\%%a\Desktop\folderx" "%USERPROFILE%\Desktop\Backup\%%a\folderx" /e /y
xcopy "%SystemDrive%\Users\%%a\Desktop\foldery" "%USERPROFILE%\Desktop\Backup\%%a\foldery" /e /y
)
Strictly speaking, not an answer, but a cleaner solution to the root problem. Still not sure how to properly strip information.

Line Removal from .CSV via Batch Script

I have 80+ uniquely named .csv files I need to remove the first 17 lines from. I am new to writing batch scripts, but would like to use this opportunity to learn.
I have been able to make this sample code work for a single file when I rename a file to test.csv, but it would be preferred if there was a "Wild Card" for file name I could use.
#echo off
set "csv=test.csv"
more +17 "%csv%" >"%csv%.new"
move /y "%csv%.new" "%csv%" >nul
File itself is updated to NOT have the first 17 lines and renames named as it is started.
try like this :
#echo off
for %%a in (*.csv) do (
more +17 "%%a" >"%%a.new"
move /y "%%a.new" "%%a" >nul
)
Here's an example based upon the For /? output, mentioned in my comment.
This one works directly at the Command Prompt:
For %A In (*.csv)Do #More +17 "%A">"%A.new"&&Move /Y "%A.new" "%A">Nul
And this is the batch file version:
#For %%A In (*.csv)Do #More +17 "%%A">"%%A.new"&&Move /Y "%%A.new" "%%A">Nul
The command above is almost identical to the other answer provided. The only 'real' difference is the use of the && conditional operator, which should only run the Move command if the previous command, More, is successful. Whilst it's an unlikely scenario that the More command should fail, I would still suggest implementing the safety of the conditional statement. If you do not feel that you require that additional feature, feel free to select SachaDee's answer, (if it solves your issue).

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.

Batch, file copy and drag and drop bug

I've been looking around for an answer to this and nothing I found could actually solve my problem - sorry if there is already an answered post similar to this one that I didn't find.
Chapter 1)
I have a batch file that is supposed to copy any file/folder (recursively) to a specific folder in my USB flash drive. So, I wrote this:
echo Copying files...
xcopy /s /i %1 \folder\Subfolder\%1
This turns out swell if I call the batch file from the command line, but it stops working if I drag the object (file/folder) onto the .bat itself. I'm sure the shell must return an error message, but I can't read it, since the command line window closes instantaneously (that's why I ran it from the command line) as usual.
Can you help me fixing this?
Chapter 2)
I'd like the batch file to be able to distinguish from two situations:
a) Copying the file "C:\Document.doc" to "\folder\Subfolder"
b) Copying the file "C:\SomeFolder" to \folder\Subfolder\SomeFolder"
My code already does b), it just doesn't distinguish from a) and when I try to copy a single file what it does is this:
the instruction
xcopy /s /i %1 \Folder\SubFolder\%1
becomes
xcopy /s /i C:\Document.doc \Folder\SubFolder\C:\Document.doc
instead of
xcopy /s /i C:\Document.doc \Folder\SubFolder\Document.doc
How can I make the batch able to correct this?
Thanks in advance!
%1 has the absolute path of the file, so when you drag a file you are telling the CMD to do this:
xcopy /S /i "C:\complete path of file\file.ext" "\folder\Subfolder\C:\complete path of file\file.ext"
You need to use the argument parameters to get the desired thing (absolute path, filename, extension, size of file, what you want)
like this:
xcopy /s /i "%~1" "\folder\Subfolder\%~nx1"
The example does:
xcopy /S /i "C:\complete path of file\file.ext" "\folder\Subfolder\filename.ext"
But really you don't need to use the second argument, you can simplify as this:
xcopy /s /i "%~1" "\folder\Subfolder\"
Because the destiny filename by default takes the same name of the original file.

Resources