Windows batch - where does this command write? - cmd

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!

Related

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.

How can I rename file extensions to another extension type, on another directory?

I'm attempting to rename all files with the file extension of ".ACH" to ".TXT" on another directory.
I used the below batch file on my local machine and it works like a charm:
#Echo Off
CD C:\Users\jonsmith\OneDrive\PC\jonsmith-PC\Desktop\TestingBatch Rename
forfiles /S /M *.ACH /C "cmd /c rename #file #fname.TXT"
However, when I attempt to use the same syntax on the server, nothing happens:
#Echo Off
CD D:\AP\ACHTest Rename
forfiles /S /M *.ACH /C "cmd /c rename #file #fname.TXT"
I'm not understanding what I'm doing wrong, but something tells me it's how I'm using the CD command.
I don't want to force Batch, so I have an easier way.
Inside file1.ach:
Hello
Inside file2.ach:
Hi
Code:
del file1.ach
del file2.ach
Current directory:
echo Hello >>file1.txt
echo Hi >>file2.txt
Different directory:
echo Hello >>D:\MyWork\TestRename\file1.txt
echo Hi >>D:\MyWork\TestRename\file2.txt
Easy, without forcing anything.

how to combine forfiles, copy, and #fname?

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"

How place file with bat command when ran from currenty directory

So I am trying to create a bat/exe installer for regjump. I am using a bat compiler so I can attach the "regjump.exe" to the installer.exe; so when the installer is ran, it will put regjump in the current directory of the installer.exe. I know I can use xcopy to place the file but I need help with the first half of the command. What is "local directory\regjump.exe" syntax?
cd C:\
xcopy "WHEREVER THE INSTALLER IS RAN FROM\regjump.exe" "C:\Windows\System32" /q /y /r
I want the syntax to be able to be ran from anywhere i.e. removable F:
Try this. You will need to execute as admin to write to "C:\Windows\System32", so this checks for that.
openfiles >nul 2>&1 || echo(You must execute this command as Administrator. & pause & goto :eof
set "SourceDir=%CD%
cd C:\
xcopy "%SourceDir%\regjump.exe" "C:\Windows\System32" /q /y /r

Batch file to delete files older than N days and save the result as a text file

I am able to create a bacth file to delete files older than n days but i am having problem with saving the deletion results. What i want is listing the deletion result in a text file after the process.
I have tried this code below. It works and delete all files. It also creates result.txt but the txt file is empty. I am seeing that files are being deleted. Any idea why they are not being saved in the text file?
forfiles -p "C:\Log\" -s -m *.* /D -1 /C "cmd /c del #path" >> "c:\result.txt"
This will provide a log of files that have been used with the del command, but not a confirmed result.
If a file is read only for example then it will still exist.
forfiles -p "C:\Log\" -s -m *.* /D -1 /C "cmd /c del #path & echo #path >>c:\result.txt"

Resources