I am using the following code in a .bat to cleanup a directory. It is to delete any directory with a time stamp older than 14 days. The thing is, this script works and deletes the appropriate directories. However it returns the error:
ERROR: The system cannot find the file specified I am unable to decipher the cause of this, and would like to get to the bottom of it.
FORFILES /S /D -14 /p %cd% /M "*" /C "cmd /c IF #isdir == TRUE rmdir #path /s /q"
As to the follow up question you asked:
Using a Windows batch file, find directories that do not contain any letters in their name. They can contain special characters and spaces. Delete the directories and their sub-folders w/o confirmation.
Put 1.bat in the directory you want to cleanup. Open a cmd window and run 1.bat.
Find all directories that do not contain any letters in their name and output their names to 1.txt. They can contain special characters and spaces.
Echo the directories to be removed. Do not remove them.
Remove comment tag to remove directories and sub-directories w/o confirmation.
1.bat
for /f "usebackq delims=|" %%a in ('DIR /b /ad ^| findstr /v /r "[a-Z]"') do echo "%cd%\%%a" will be removed without confirmation.
:: for /f "usebackq delims=|" %%a in ('DIR /b /ad ^| findstr /v /r "[a-Z]"') do rd /s /q "%cd%\%%a"
set /P source=C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Config
set /P destination=C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Hi
set xcopy=xcopy /S/E/V/Q/F/H/I/N
%xcopy% %source% %destination%
this does not work.
Can anyone tell me what's wrong ?
Update:
The following code works but it creates the entire directory structure inside the destination. I only want the files to be copied.
xcopy /s /e /y "C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Config" "C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Hi"
pause
You need to remove the /p following the set commands. Also for the xcopy command you included switches that contradict themselves, ex. /s and /e. Look up the documentation on them.
You also need to change the variable name of xcopy to something that isn't already an inbuilt command.
set source=//FileLocation//
set destination=//FileDestination//
xcopy %source% %destination% /E /V /F /H /N
This should copy the files over with the directory structure. If you are still looking for only the files then I would recommend using a for loop to loop over all the files and transfer them that way. See here How to copy only files(not directories) using batch file?
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
S:
cd \newclients
xcopy "s:\clients\*\MER" . /s /d
pause
This is my batch file. In my folder tree we have clients then names folders then MER folder. I want to be able to search the directory for the MER folder and copy that folder along with the client name before it. Is there a way to do this with batch files?
Using if exist should do the thing.
pushd "s:\newclients"
for /f "delims=" %%f in ('dir /b /ad "s:\clients\*"') do (
if exist "s:\clients\%%f\MER\nul" ( xcopy "s:\clients\%%f\MER" "s:\newclients" /s /d )
)
popd
pause
Note: Add a slash if it's to check folder ie. if exist "s:\clients\%%f\MER\" ... or if exist "s:\clients\%%f\MER\nul" ...
To suppress prompt about destination file/folder add /I in xcopy :
/I If in doubt always assume the destination is a folder
e.g. when the destination does not exist.
https://stackoverflow.com/a/33445866/
I have a directory which looks like this:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\scannedDocs\2004\2009\jan\file2.pdf
\isa\documents\2005\2008\jac\file1.txt
\isa\documents\2003\jan\file1.txt
\isa\scannedDocs\2002\2009\jan\file2.pdf
I have a list of files I need copied (exported from a database), but because I don't need EVERY file in the directory, I only want the ones copied from my list:
Files-needed.txt:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\documents\2004\jac\file3.txt
\isa\documents\2003\jan\file1.txt
Basically:
I'll need to copy out the specific files needed, and
Log when a file can't be found, and,
I need folder structure retained, as the files might have the same name, just in different directories.
It's on a windows 7 machine, and I can run PowerShell and batch files. I tried robocopy and xcopy and either got all of the directories and no files or all files and no directories...
Any assistance would be great.
EDITS:
Okay, so i have tried Robocopy, but it is either copying the directories and no files or files and no directories. I haven't tried anything in powershell yet, but that might be the way...
It is Windows, I might have just written the slashes incorrectly above, I work between both environments, and was just trying to explain the problem.
Things I tried:
#echo off
set src_folder="C:\batchScripting\TEST_DIR\"
set dst_folder="C:\batchScripting\COPY2_DIR\"
robocopy "C:\batchScripting\TEST_DIR" "C:\batchScripting\COPY2_DIR" FileList.txt /S /V /NP /LOG:"log.log" /R:10 /W:30
and
#echo off
set src_folder=C:\batchScripting\TEST_DIR\
set dst_folder=C:\batchScripting\COPY2_DIR\
for /f "tokens=*" %%i in (File-list.txt) DO xcopy /s /i "%src_folder%\%%i" "%dst_folder%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (q23221996.txt) DO (
IF EXIST "%sourcedir%%%a" (ECHO f|xcopy /y "%sourcedir%%%a" "%destdir%%%a" 2>NUL >nul
) ELSE (ECHO "%sourcedir%%%a" does NOT exist)
)
GOTO :EOF
I used a file named q23221996.txt containing your data for my testing. sourcedir and destdir are both set up to suit my system.
The /y on the xcopy command forces overwrite if the destination file already exists.