Why isn't this xcopy command not working? - windows

I am trying to run a simple xcopy batch script but I am facing some difficoulties.
Here is the script
SET SRC=C:\FOLDER1\FOLDER2
SET DEST=V:\FOLDER1
FOR /D %%d in (%SRC%\*) do xcopy /S /I /y /exclude:%SRC%\exclude.txt %%d V:\FOLDER1\%%~nxd
Basically this script should copy some of the subfolders of C:\FOLDER1\FOLDER2 (excluded.txt contains a list of directories to exclude) and its content to the destination. However when I run the scripts, altough no errors are thrown, NO FILES OR FOLDERS get copied. What am I doing wrong?
Interestingly if I run the following script INSIDE FOLDER2, everything procedes as expected.
FOR /D %%A in (*) DO xcopy /S /I /y /exclude:exclude.txt %%A V:\FOLDER1\%%A

xcopy "C:\FOLDER1\FOLDER2" "V:\FOLDER1" /e /i /y
You mention nothing about excluding any folders.

Related

looping through a directory in bat, going up a directory using bat file

I have a bat file which part of looks like:
set test=test
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F
xcopy ..\dir c:\path\%test%\dir\ /S /E /F
The for loop does not work, but the xcopy does. If I move the contents of the directory above to the current directory and change the code to remove the "..\":
for /d %%x in (*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F
it works. Can someone please tell me why the bat script in the for loop cannot look up a directory? Am I approaching this wrong?
EDIT: I have now changed the command to after seeing ths answer but it still does not work:
for /d %%~nxx in (..\*.%MUI%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F
I receive the error:
%~nxx was unexpected at this time
EDIT #2:
I still cannot get it to work, my commands have looked like
for /d %%x in (..\*.%test%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\temp\%test%\%%~nxx\ /S /E /F
It can. But %%x will contain ..\xyz.test, not xyz.test, which is probably not what you want in your xcopy target.
Replace it with %%~nxx (for "Name and eXtension of x") to chop of the path.
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%~nxx\ /S /E /F

XCOPY still asking (F = file, D = directory) confirmation [duplicate]

This question already has answers here:
Why does the command XCOPY in batch file ask for file or folder?
(11 answers)
Closed 3 months ago.
My batch script xcopy is still asking F = file, D = directory confirmation even though I have added /F in the script, the log is showing as below.
Please help on how to avoid asking confirmation.
Script:
net use p: /delete
net use p: "\\200clan\F_Drive" /USER:adm /PERSISTENT:NO-1111
set source=%1
set target=p:/%2
echo %source% %target%
xcopy /S /I /Q /Y /F "%source%" "%target%"
Log:
C:\test\foldera>xcopy /S /I /Q /Y /F "C:/test/folder1/folder2/logs/154/compareReport_177.html" "p:/Services/WSDLCompare/177_20151116/compareReport_177.html"
Does P:\Services\WSDLCompare\177_20151116\UIReport_177.html specify a file name
or directory name on the target
(F = file, D = directory)?
The /I switch (not /F as you mentioned in your question) prevents xcopy from asking whether the destination is a file or a directory only if multiple source files are given, so if the source is a directory, or if wildcards ? or * are used. If the destination already exists, such prompt does never appear.
There are the following scenarios (depending on the provided values of %source% and %target%):
a single source file, the destination is a file:
the /I switch is useless, so you need to pipe F into the xcopy command line:
echo F|xcopy /S /Q /Y /F "%source%" "%target%"
provided that the /Y switch is given (to force overwriting), you could also create the target file in advance (empty file):
>> "%target%" rem/
xcopy /S /Q /Y /F "%source%" "%target%"
a single source file, the destination is a directory:
the /I switch is useless too; you can pipe D into the xcopy command line:
echo D|xcopy /S /Q /Y /F "%source%" "%target%"
or you can simply append a \ to the destination:
xcopy /S /Q /Y /F "%source%" "%target%\"
although this causes trouble when %target% specifies the current directory of a drive like D: for instance, because D: means the current directory of this drive whereas D:\ means the root directory of it;
or you create the destination directory in advance:
2> nul mkdir "%target%"
xcopy /S /Q /Y /F "%source%" "%target%"
the 2> nul portion suppresses the error message in case the directory already exists;
multiple source files, the destination is a file:
this is usually a senseless situation, because you tell xcopy to copy each source file to the same destination file, thus attempting to overwrite it;
multiple source files, the destination is a directory:
the /I switch makes sense here:
xcopy /S /I /Q /Y /F "%source%" "%target%"
the pipe option also works here:
echo D|xcopy /S /Q /Y /F "%source%" "%target%"
so does appending a \ to the destination (regarding the limitation as mentioned above):
xcopy /S /Q /Y /F "%source%" "%target%\"
or you create the destination directory in advance:
2> nul mkdir "%target%"
xcopy /S /Q /Y /F "%source%" "%target%"
Conclusion
The most flexible and secure solution is to pipe the desired selection (F or D) into the xcopy command line. (Note that the query is locale-dependent.)
Supplement
There are some minor issues in your code fragment I want to mention here:
you should generally use the \ as a path separator as this is the Windows standard character for that purpose (although / works too in most cases);
there is -1111 appended to your second net use command line; if this constitutes the password for the resource, it should be moved before the /USER option; otherwise just remove it;
your set command lines introduce problems with some special characters (like &, ^, (, )); to avoid such, state set "source=%~1" and set "target=p:/%~2"; the ~ removes potential surrounding "" from the arguments (which are required if they contain SPACE, ,, ;, =);
Here is the code with the all of the above things reworked:
net use P: /DELETE
rem supposing `-1111` constitutes the password for the resource:
net use P: "\\200clan\F_Drive" -1111 /USER:adm /PERSISTENT:NO
set "source=%~1"
set "target=P:\%~2"
echo "%source%" "%target%"
rem supposing the destination is a directory:
echo D|xcopy /S /I /Q /Y /F "%source%" "%target%"
rem actually you do not need any interim variables:
REM echo D|xcopy /S /I /Q /Y /F "%~1" "P:\%~2"
Put a \ behind the target to suppress the question and specify it as a folder:
xcopy src dest\
Put a * behind the target to suppress the question and specify it as a file:
xcopy src dest*
Note: The second command is a hack that relies on wildcard matching (will match any item starting with "dest") so use it at your own risk!
xcopy doesn't know the target is a directory. You clarify this by putting a backslash at the end:
xcopy /S /I /Q /Y /F "%source%" "%target%\"
When copying a single file with XCOPY, there is no option to indicate if the destination is a filename or a directory (with the filename defaulting to that of the source file).
In such cases XCOPY will prompt with a (locale specific) message like:
C:> xcopy foo.txt bar.txt
it prompts
Does foo.txt specify a file name or directory name on the target (F = file, D = directory)?
Adding a wildcard (*) to the end of the destination will suppress this prompt and default to copying as a file:
C:> xcopy foo.txt bar.txt*
1 File(s) copied
This requires the source and target file extensions to be the same length, typically 3 characters.
for more information: https://ss64.com/nt/xcopy.html
Removing the destination filename will suppress the message. This works for me!
I use:
xcopy /S /I /Q /Y /F "%source%" "%target%"
It works.
Try:
echo F>xcopy_answer.tmp
xcopy xcopy /Q/Y "%source%" "%target%"<xcopy_answer.tmp

Batch copying to all folders

I need to batch copy two folders, let's call them A and B, from F:\Sourcefolder\ to F:\destinationfolder subfolders (not to destination folder itself).
Now I know when batch copying file (file.exe for example) it is supposed to look something like this
for /r "F:\destinationfolder" %%i in (.) do #copy "F:\Sourcefolder\file.exe" "%i"
In each of those subfolders there is a lot of files. After copying A and B folders to all subfolders, I would like to move all files within the subfolders to folder A within their folder. Is this possible to do?
the XCOPY command is designed for folder copy, FOR /D will list level1 folders:
for /d %%a in ("F:\destinationfolder\*") do (
XCOPY "F:\Sourcefolder\A\*" "%%~fa" /s /i
XCOPY "F:\Sourcefolder\B\*" "%%~fa" /s /i
)
for recursive copy (all subfolders):
for /r /d "F:\destinationfolder\" %%a in (*) do (
XCOPY "F:\Sourcefolder\A\*" "%%~fa" /s /i
XCOPY "F:\Sourcefolder\B\*" "%%~fa" /s /i
)
FOR /R will not work properly if there's no wildcard in the brackets - ? or *
ROBOCOPY,XCOPY

how to traverse specified subfolders in a windows batch file?

Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.
Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.

xcopy all folders and subfolders from txt(with paths , and spaces )

I have a txt file with the full path for .jpg files, I need to xcopy the whole folders including everything inside using xcopy using batch file
This is an old question, but I had the same question and neither of the above answers quite did it for me. I had to add /s:
xcopy C:\path\to\source\directory D:\path\to\destination\ /e /i /y /s
That copys all files, subfolders, and files in subfolders. More (and helpful) documentation available here:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy
xcopy /? will show you the options you can use. You probably want something like xcopy C:\path\to\source\directory D:\path\to\destination\ /e /i /y
Something like this should do it
FOR /F "delims=" %%i IN (c:\temp\paths.txt) DO xcopy "%%i*.jpg" "C:\test\" /s /y

Resources