If I try this command:
forfiles /M *.WAV /C "cmd /c rename #file temp.WAV"
I get this error :
a duplicate file name exist, or the file cannot be found
I want that like windows i can add to all the rest files that exist a number like that:
temp.WAV
temp (2).WAV
temp (3).WAV
temp (4).WAV
temp (5).WAV
I tried several time and method and didn't success:
forfiles /M *.WAV /C "cmd /c rename #file temp.WAV
a duplicate file name exist, or the file cannot be found
Related
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!
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.
I have multiple zip folders with unique names but the files within the folders all have the same name. I need to rename the text files to match the folder names. I also need those renamed text files to be moved to the working directory.
For example, I have the following:
name1.zip decompresses to name1\AncestryDNA.txt but need to be name1.txt.
name2.zip decompresses to name2\AncestryDNA.txt but need to be name2.txt.
I used 7-Zip 19.00 to unzip the files. I decompressed the files because I also need to remove lines starting with "#" from each decompressed text file for use in another program.
I start with a windows batch file the runs the following on my Data directory that contains my zip files:
echo Unzipping the files...
forfiles /p Data /m *.zip /c "cmd /c 7z x *.zip -o* -y"
echo Moving zip files to storage...
forfiles /p Data /m *.zip /c "cmd /c move #file ..\StorageZipFiles"
echo Trimming all except header from AncestryDNA text files...
forfiles /p Data /s /m *.txt /c "cmd /c sed -i '/#/d' #file"
These work but I can't get the rename function to work.
I tried to rename the files before decompressing...
forfiles /p Data /s /m *.zip /c "cmd /c 7z rn #file #fname\AncestryDNA.txt #fname\#fname.txt"
and...
forfiles /p Data /s /m *.zip /c "cmd /c 7z a rn #file #fname\AncestryDNA.txt #fname\#fname.txt"
and after decompressing...
forfiles /p Data /s /m *.txt /c "cmd /c ren #file #fname.txt"
but I have not been successful with carrying the name of the folder over to the text file within the folder.
When trying to rename the files after decompressing, the command prompt does not produce an error message. It keeps the old file name of AncestryDNA.txt nested within the folders name1, name2, etc.
Another way of renaming the files after decompression is this powershell script.
Get-ChildItem -recurse -path Data *.txt | rename-item -path {$_.fullname} -newname {$($_ | split-path | split-path -leaf ) + ".txt"} -whatif
This has two interleaved powershell pipelines, one gets all the right directories and the second pipeline extracts the name of the folder used to rename the file.
Note that I put a -whatif at the end of this for safety. If the output looks good then remove the -whatif to actually change the filenames. It is still best to test this on test data before using on real data.
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"
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"