Windows batch autoanswer XCOPY /i - windows

I have created a batch file which uses FOR command to read a file of FROM Directory, FROM File, TO Directory, TO File as the parameters. (I am giving the files NEW names in the destination)
Everything works great until I add a new file to the mix.
In XCOPY /i option says it is a directory (which is NOT true). IF I don't use /i it wants to know if it is a file or a directory. It is ALWAYS a File. Is there a way I can autoreply or does someone have another suggestions.

echo f|xcopy [options] [files*]

A short investigation did not yield an easy command line parameter solution.
If I got you right, you are trying to copy a file from one directory to a new directory + give it a new name in the new directory. Copying to the new directory should work fine when you terminate target directory name with a tailing '\' (backslash) - this should result in a file with the same name in the target dir. Renaming this file to the new name will be straight forward. However, it's two commands instead of one...
In case this does not work for you: maybe you can illustrate your qn with a sample / snippet of the batch file?

Related

"Can't read file" when trying to Exclude directory in batch file

I've been stuck on this for awhile and could use some help.
I'm trying to copy a large folder from a mapped network drive (A:) onto my local PC. I also need to exclude a subdirectory on that drive path called "Images". My current code (backup.bat) is below:
cd %HOMEPATH%\Desktop\%mydate%
xcopy "A:\PROGRA~2\QuadTech" 121\ /e /EXCLUDE:"A:\PROGRA~2\QuadTech\INSPEC~1\Images\"
Error I keep getting:
I've tried shortening the path with "dir /x" and I am sure the path name is correct. Also note that I need quotations as there are spaces in the PATH name.
Why am I getting errors when trying to Exclude this directory??
ANSWERED
I now have my Exclude statement point to my desktop where it reads a list of strings in a txt file.
xcopy "A:\PROGRA~2\QuadTech" 121\ /e /EXCLUDE:C:\Users\QuadTech\Desktop\excldelist.txt
Txt file contents:
\Images\
This is happening because the /EXCLUDE option does not specify files to exclude.
It specifies files containing lists of files to exclude.
More info by typing xcopy /?, though I am sure you know that.
(I know, I missed it too in the beginning; sometimes it is just a matter of having a second pair of eyes.)

Shell script to copy and paste a particular file from one directory to another?

As part of a Windows software project I'm working on I need to copy and paste a particular DLL file from directory A to directory B. In the process, I need to overwrite any previous version of this file in directory B.
Directory A is: Documents/Visual Studio/2015/Project/MyProject/MyProject/bin/Debug
Directory B is: This PC/Windows(C:)/Users/Public/Public Documents/B
Is there a shell script that can perform this operation?
Any help would be greatly appreciated!
Described goal can be achieved by single cmd command:
copy /Y "%USERPROFILE%\Documents\Visual Studio\2015\Project\MyProject\MyProject\bin\Debug\libName.dll" "C:\Users\Public\Public Documents\B"
So you can put this command into scriptName.bat file and execute. Put pause at the end of the file to prevent window closing if you want to check script output.

How do I find current directory for my Batch file?

I am currently working on a batch file (let's call it temp.bat). I am trying to move the batch file to my desktop, but I can't find my batch file location / directory. I am asking if there is an extension or something that can use to automatically identify the directory, instead of entering it every time. In other words, I can still move the file to another place even in another directory. Example:
temp.bat is currently on my Downloads file. I use the move command to move the file to Desktop. Now temp.bat is currently on my Documents file. I try to use the same command to move the file to Desktop.
Is there any parameter extensions for it?
This is what I have now:
#echo off
move /y [The directory of temp.bat] C:\Users\69Swag\Desktop
All answers appreciated! Thanks :)
You don't need the current directory to move a file.
move temp.bat C:\Users\69Swag\Desktop
If you still want the current directory, use this:
echo %cd%

Batch file to detect only part of a filename

I need a snippet of a batch file that can detect part of a filename, then rename it.
Note that the numbers after the filename change randomly but "file" is always the same.
Example:
filename: file143424
There will always be only one file that needs to be renamed in the folder. The script will delete older versions of the file. For instance, I put a first file inside the folder and the script in there too. I then run the script, which renames it to just file. Then if I put a new file in, the script, when run again, will recognize it from the "file" prefix and rename it to file after deleting the old one.
Excuse me. Your question is incomplete, I think. You want to rename "file134342" to what?
In your comment you said: rename it to just "file", but this works only if there is just one file with that name. Anyway, here it is:
for %%f in (file*) do ren %%f file
If this is not what you want, then give us more details (you showld always give full details from the very beginning).
You can check for the first four characters that way:
if "%filename:~0,4%"=="file" ...
#Aacini's suggestion should work fine in your case. But you could do very well without a for loop, a single REN (RENAME) command would be enough:
RENAME file* file

Can we use robocopy to copy files instead of folders

I need to copy a particular file from one location to another., Is it possible to use robocopy to do same.
Whilst Robocopy can be persuaded to copy a single file it is much simpler to use copy or xcopy.
Yes, either wrap it in an an exec or use the Robocopy that is wrapped as part of the msbuild extension pack see:http://www.msbuildextensionpack.com/help/4.0.3.0/index.html
You want to use the MSBuild.ExtensionPack.FileSystem.RoboCopy task.
Makes copying much quicker.
I was trying to figure this problem out. I finally found my own solution and maybe it will help.
I noticed that the syntax used to select the entire directory could be used to select a single file.
ROBOCOPY "*" "Directory source" "Directory Output unc path or non"
The above code will copy everything from the directory source folder to the directory output path.
Let's say you only wanted to copy 1 file from the directory source named "test.txt"
In order to do that use the following code:
ROBOCOPY "*test.txt" "Directory source" "Directory Output unc path or non"
Thats about it. It works really well and will only copy the file name you want.
Alternatively you can use
ROBOCOPY "*.txt" "Directory source" "Directory Output unc path or non"
to copy out all text documents from the directory source.
Similarly this will also work with any .ext
.zip .exe .txt .pdf etc..
I signed up to answer this question with a better method. Let me know if I succeeded.

Resources