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.
Related
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
I'm new to PowerShell, and am trying to move a few excel spreadsheets into a folder titled "old folder" using the forfiles command. PowerShell says that the files have been moved, but when I check the folder they're not there. They aren't in their original location either.
forfiles /P C:\Users\NewUser\Desktop /M *.xlsx /D +27/05/2019 /C 'cmd /c move #file C:\Users\NewUser\Desktop\"old folder"\'
I tried changing the folder name to a single word "oldfolder" and it worked just fine. What am I doing wrong?
There seems to be a problem with the syntax of your /C parameter. As far as I can tell the argument should be enclosed in double-quotes. And because of the space in the file name that should also be enclosed in double-quotes. Quotes within quotes should be escaped with backslash.
I would suggest this:
forfiles /P C:\Users\NewUser\Desktop /M *.xlsx /D +27/05/2019 /C "cmd /c move #file \"C:\Users\NewUser\Desktop\old folder\""
As forfiles has it's idiosyncrasies even in cmd, I wouldn't drag that along to PowerShell.
IIUR that translates to:
Get-ChildItem 'C:\Users\NewUser\Desktop\*.xlsx' -File |
Where-Object LastWriteTime -ge [DateTime]::Today |
Move-Item -Destination 'C:\Users\NewUser\Desktop\old folder\'
Not sure if you meant that to be /D -27/05/2019 => -lt or -le instead of -ge
Let's say we have:
somedir
file1.eps
file1.jpg
file2.eps
file2.jpg
What I want to do is, to get zips of every eps, jpg couple with the same names like:
somedir
file1.zip
file2.zip
I can't figure out how to do this. Any suggestions?
You can do it easily with help of WinRAR:
#setlocal
#echo off
set path="C:\Program Files\WinRAR\";%path%
forfiles /s /m *.jpg /C "cmd /c winrar.exe a -afzip "#fname.zip" "#fname.eps" "#fname.jpg"
pause
Here is my example with windows native zip.exe. It looks for only *.eps files inside of current directory (because you have pair of files with the same name and different extensions), then it extracts filename, adds .eps and .jpg extensions and zip them with same name.
for /r %i in (*.eps) do zip %~ni.zip %~ni.eps %~ni.jpg
If you write inside of script then white with %%i
for /r %%i in (*.eps) do zip %%~ni.zip %%~ni.eps %%~ni.jpg
I have a problem with forfiles syntax. I want to write a simple forfiles command, that will:
Select files, that were modified in January,February,March, etc.
Move these files to another folder.
but I'm having problems to write proper syntax. Here is what I wrote at this point.
forfiles /m *.file /d (What do I write here?) /C "cmd /c move #file C:\Users\User1\Desktop\New folder"
Date modified format is: DD/MM/YYYY.
I don't think what you want is possible with forfiles unless you can process the list of months in reverse order (first move files with /d +07/08/2016, then with /d +01/07/2016, then /d +01/06/2016, and so on).
If PowerShell is an option you may have better luck using that, e.g. like this:
Get-ChildItem *.file |
Where-Object { $_.LastWriteTime.Month -eq 8 } |
Move-Item -Destination 'C:\Users\User1\Desktop\New folder'
To filter out all files modified in a certain month (June 2016 in the following example), you could use two nested forfiles loops, where the outer one passes all files modified on or after the first day of the month to the inner one, which returns all items out of them that are modified on or before the last day of the month:
> nul 2>&1 forfiles /D +01/06/2016 /C "forfiles forfiles /M #file /D -30/06/2016 /C 0x22cmd /C if #isdir==FALSE > con echo #fdate0x09#ftime0x09#path0x22"
This script returns a list of matching files with their modification dates and times. To move the files to another directory (for instance, C:\Backup\June-2016), the command line looks like this:
> nul 2>&1 forfiles /D +01/06/2016 /C "forfiles forfiles /M #file /D -30/06/2016 /C 0x22cmd /C if #isdir==FALSE move 00x7822#path00x7822 00x7822C:\Backup\June2016\\#file00x78220x22"
The redirections avoid empty lines and error messages to be output by forfiles.
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"