how to combine forfiles, copy, and #fname? - windows

This command is supposed to copy multiple files from a static source folder into each folder for a set of saved web pages:
forfiles /m *.htm /c "cmd /c copy /y _core/*.* #fname_files"
However, each call fails with a status of, "The system cannot find the file specified."
If this is tried:
forfiles /m *.htm /c "cmd /c copy /y 0x22_core/*.*0x22 #fname_files"
the status displayed shows the name of each source file and the same error message.
I've also tried adding setlocal/endlocal around the call but it still fails.
Searching on the web brought lots of discussions but nothing showing forfiles, cmd, and copying into a destination directory using #fname.
Would someone with deeper knowledge of batch scripting "fix" this line so it works as intended?

If I understand correctly what you are trying to do :
1)You might be getting "The system cannot find the file specified." because the directory _core is not in the same directory as the *.htm files
2)In order to copy the *.htm files into each #fname_files folder, you must create the folder first. Here is the command line with mkdir added :
forfiles /m *.htm /c "cmd /c mkdir #fname_files & copy /y _core\*.* #fname_files"

Related

Windows batch - where does this command write?

I used this command in a windows command line:
C:\Users\myuser\Desktop>C:\Windows\System32\ForFiles.exe /P C:\myfolder\mysubfolder /S /M *.* /D +09/15/2022 /C "cmd /C echo #FSIZE >> sizes.txt"
I wanted to echo all the sizes for files in folder modified in the last 5 days.
I didn't found the output file.
I then solved the problem by changing the command to:
C:\Users\myuser\Desktop>C:\Windows\System32\ForFiles.exe /P C:\myfolder\mysubfolder /S /M *.* /D +09/15/2022 /C "cmd /C echo #FSIZE" > sizes.txt
Anyway, I'd like to know if I created a sizes.txt file somewhere on my hard drive.
Searched in the folder, subfolder, desktop, home folder, C:, C:\Windows, C:\Windows\System32... nothing...
I finally found them, yes "them".
One in each directory containing recently edited file(s).
Seems that the command forfiles executes is actually run where the file is located.
It's strange because if you specify cmd /C echo %CD% as command it actually prints the directory you run from, in my case Desktop!

Scheduled BAT - Error during XCOPY if file is in use

I have this script scheduled every hour:
#echo off
set path1="E:\Document\Backup"
set path2="E:\Document\NewDoc"
set path3="C:\ScanDoc"
forfiles -p %path1% -s -m *.pdf /D -30 /C "cmd /c del #path"
xcopy %path2%\*.pdf* %path1% /c
start /d %path3% ScanBatch.exe
Files in "NewDoc" folder are created by manual document scanning (PDF FORMAT), so sometimes documents are in use.
The Scanbatch program read files in "Backup" folder, so if PDF is copied from "NewDoc" to "Backup" while in use, it's result as corrupted and the Scanbatch go in error.
Is there a way to copy files ONLY IF NOT IN USE?
At the end the real problem wasn't xcopy, but "Scanbatch.exe" that crashes if found an opened file. Problem solved changing schedulation time.

copying all the elements of a folder into another folder using a loop in batch file

I want to copy all the files of a folder into some other folders using batch script. Say, I have two folders named folder1 and folder2. these two folders are located in C:\Users\xyz . I want to copy the elements of another folder (say, folder3 which is located in C:\Users\abc\def) into these two folders. I have made the following script but nothing is copied. My sample batch file is as follows:
FOR /L %%A IN (1,1,2) DO (
xcopy /s C:\Users\abc\def\folder3 C:\Users\xyz\folder%%A
)
is there anything wrong in the batch file?
xcopy /s C:\Users\abc\def\folder3\*.* C:\Users\xyz\folder%%A\
where *.* is an appropriate filemask and the final \ in the destination name tells cmd that the destination is a directory.
I suggest using this command line in the batch file:
for /L %%A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%%A\" /C /G /H /I /K /R /Q /S /Y >nul
I enclosed both directory paths in double quotes in case of real paths contain 1 or more spaces or other special characters which require double quotes. The last paragraph on last help page output by running in a command prompt window cmd /? outputs on which characters in a directory/file name double quotes are required around the complete directory/file name.
The target path ends with a backslash to make it clear for console application xcopy that the target is a directory and not a file. Together with the redundant /I the target directory is created if not existing already.
For details on the options used on xcopy open a command prompt window and run xcopy /?. This outputs the help for this console application in the command prompt window. On Windows running a command or console application with /? as parameter outputs in general the help for the command/application.
Note: The copying from one user profile directory to another user profile directory requires local administrator privileges. Each user profile directory is by default protected for exclusive usage of the owning user. Therefore I suggest to open a command prompt window and execute in this window:
for /L %A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%A\" /C /G /H /I /K /R /S /Y
You can see if that works with just %A as required on command line instead of %%A as required in batch files and without /Q (quiet copying) and without >nul (redirection of success messages to device NUL to suppress them). Or when it does not work, you can see why it does not work as the error message can be viewed on running a command or a batch file from within a command prompt window instead of double clicking on a batch file because the console window keeps open.

Date and time a file was copied to a directory

Is there a way to see what time a file get copied to a directory? Looks like Date modified column in windows explorer shows the date and time when the file got created.
You could try checking this via the command line.
Here are some commands that may help.
Using 'dir'
This, for example, gives the last modified time of this file:
dir /T:W d:\test.pdf
And this gives the date/time for all files and sub-directories in the current one:
dir /T:W
Using 'forfiles'
Modified datetime for all files in current dir:
forfiles /C "cmd /c echo #file #fdate #ftime"
Or just the pdf files within the directory:
forfiles /M *.pdf /C "cmd /c echo #file #fdate #ftime"
Using 'cp' in linux
I don't know who useful this would be for you, but on Linux machines you can use this option when coping files:
$ cp --no-preserve=timestamps my-file.pdf my-copy.pdf
This way the copy will have its own creation time and won't depend on the original file.

Windows cmd shell xcopy to network directory doesn't work

Im trying to make a batch file that will copy all new files and folders from a source folder to an network directory. All the new subdirectories and new files should be copied (backup).
My code:
xcopy "C:\Source" "T:\Backup" /d/i/s/q
(/d for only new files, /i because source is a dir, /s for all the subdirs and files, /q just to supress the copy text)
Source contains both subdirectories and files (.txt).
The first run it copies Everything as it should. When I add a new .txt file to one of the existing subdirectories and run it again I get the message:
"An error occured when the file The directory is not empty. was being created.
The folder "T:\Backup" could not be created.
0 files copied.
(Translated from Swedish so not 100% original)
The thing is when I try this command to a local source like e.g. "C:\test" and do the same procedure it works.
Anyone who can understand why this doesn't work for the network drive?
Should I try Another command such as robocopy?
Skip xcopy and use robocopy with the /E flag instead. It's built into all recent versions of Windows. Free download for XP.
Example:
robocopy c:\source T:\backup /E
That will copy all the files in the "source" folder to the "backup" folder that haven't been copied already.
And if you don't want to have the output shown on the console (equivalent to the /Q option in xcopy):
robocopy c:\source T:\backup /E /LOG:nul
Robocopy must be better because it should create directories with the \E switch. No overwrites for files, just adds a file with extra letters or extension <> command. Still must defrag.
XCOPY "DRIVE LETTER:\windows.old\USERS" "\computername\D\NAME\" /D /E /C /R /I /K /Y /f

Resources