Batch - Extract many zip, rename files extracted in a same directory - batch-file

Hi, I have many zip files located at g:\toto. These zips contain some files. I would like to extract all zip in a same directory (g:\toto\extracted) then rename various files of the zip.
Example 1 :
www_12567.vp.zip : 3 files : alpha.doc, beta.xls, teta.doc
I would like after extraction, files are renamed with the name of the zip
www_12567.vp.alpha.doc, www_12567.vp.beta.xls, www_12567.vp.teta.doc
Example 2 :
www_12.vp.zip : 3 files : al.doc, bea.xls, tta.doc
www_12.vp.al.doc, www_12.vp.bea.xls, www_12.vp.tta.doc
I found this question, but it talks about .txt and the zip contain one file, so, it doesn't work.

Without knowing the contents of the archive you can't know which files to rename, because you are putting them into a directory that may already contain other files.
This, however, would be much easier if there was a dedicated directory to put the files temporarily. Here's how you could use it:
#ECHO OFF
SET "srcdir=G:\toto"
SET "tgtdir=G:\toto\extracted"
SET "tmpdir=G:\toto\extracted-tmp"
FOR %%Z IN ("%srcdir%\*.zip") DO (
unpack "%%Z" with your favourite tool into "%tmpdir%"
FOR %%I IN ("%tmpdir%\*") DO MOVE "%%I" "%tgtdir%\%%~nZ.%%~nxI"
)
Of course, the temporary directory would need to be empty before running the batch file. You could add DEL "%tmpdir%\*" somewhere before the loop to make sure it is.
One other note is, the above assumes that the archives do not contain subdirectories or, at least, that the files are extracted without subdirectories.
UPDATE
If you are using the 7-Zip archiver to work with .zip files, then this is how your extract command might look:
7z e "%%Z" -o"%tmpdir%"
Disclaimer: I'm not an active user of 7-Zip. This is what I used as a reference to come up with the above command:
7-Zip Command-Line Examples

Related

How to rename PDFtk-server files with timestamp?

I am using PDFtk-server to merge 2 PDF files. This creates a new PDF with name Test_.
Now, what I am looking for is a batch files that renames the Test_ files to Test_datestamp and then move them into another folder and delete the files.
It is working if only one file is present.
pdftk C:\test\*.pdf C:\test\file-to-add\file.pdf cat output C:\test\output\Test_.pdf
ren "C:\test\output\*pdf" "Test_ - %date:/=.% %time::=-%.pdf"
del C:\test\*.pdf
Then comes to copy part to other folders.
If having multiple files, the first one gets renamed but other not, because it is trying to set the same timestamp, so I need some kind of delay between renaming.

Check if file exists in 2 directories, delete files that aren't in both directories

So, I'm programming a game, but the compiler I use is written as a Windows batch file. I'm using Windows 10 as my operating system.
In my game files, I have one folder with images, and another folder with upscaled versions of those images that have the same file name and extension.
What I want to do is have the batch file go through all the images in the directory with the upscaled images, and check if a file with the same name and extension exists in the directory with the original images. If it doesn't exist in the original directory, it will delete it from the upscaled directory.
I got it working using an answer over here:
Batch Extract path and filename from a variable
All I had to do was extract the file name and extension, and then run all the files in one folder through the loop, checking if the file exists in the other folder.
Here is my code:
for %%i in (%folder1%\*.png) DO (
if not exist "%folder2%\%%~nxi" ECHO %%~nxi
)
If you actually want to delete the files, change ECHO to del /q, be warned you will lose files, so make sure you have backups.

Rename file names with folder names (maybe use Python)

I have a parent folder that contains multiple folders within it. Then, each of these nested folders contains 4 files that make up a GIS shapefile and have different extensions (i.e., ".dbf", ".prj", ".shp", and ."shx"). I am new to coding (outside of R) and do not know whether this can be automated with Python or if I need to run a shell script (I'm working on a windows). I have very rudimentary coding schools so documentation would be great (and/or suggestions of "dummy" sites to read).
Here is an example of the current file structure (showing the four files I want to rename with the subfolder name):
Parent Folder: "Raptors"
Subfolder: "Falco_peregrinus"
File 1: "ra03310.dbf"
File 2: "ra03310.prj"
File 3: "ra03310.shp"
File 4: "ra03310.shx"
Here is what I would like the four files to be renamed to:
File 1: Falco_peregrinus.dbf
File 2: Falco_peregrinus.prj
File 3: Falco_peregrinus.shp
File 4: Falco_peregrinus.shx
Thanks.
For a batch file solution
#echo off
for /d %%a in ("c:\...\Raptors\*") do ren "%%~fa\*.*" "%%~na.*"
For each folder inside the parent one, rename all the files inside the folder to the name of the folder but keeping the extension
for command is used to iterate over the list of folders (/d) under the parent folder. For each of the folders, the replaceable parameter %%a will hold a reference to the subfolder and the code in the do clause is executed for each one.
The code in the do clause executes a ren command, for all the files under the subfolder (%%~fa is the folder being processed with full path), changing its name to the name of the folder (%%~na).
edited The answer is not completely correct. While the basic idea of using only one ren command to rename all the files under each folder is probably the fastest way, the way ren command handles wildcards makes this code fail if the folder name contains dots. To be sure the code will not fail, it is necessary to iterate over the files, renaming each one
for /d %%a in ("c:\...\Raptors\*") do for %%b in ("%%~fa\*") do ren "%%~fb" "%%~nxa%%~xb"
For each folder (%%a), for each file inside the folder (%%b), rename the file to the name of full folder name (%%~nxa) with the extension of the file (%%~xb)
You could use almost any programming language (probably including R) to do this. Python is a good choice here because it has such friendly syntax.
A extremely simple script that will solve your problem might look like this
import os
import os.path
'''
Given a file name, returns a pair with the name and extension (hello.txt => [hello,txt])
'''
def split_name(file_name):
return file_name.rsplit('.',1)
'''
Recursively renames files in subdirectories of base_directory so each file is named the subdirectory name plus the extension
WARNING! You will be very sad if you have multiple files with the same extension in any of those folders
def rename_file(base_directory):
#Get the folder name for base_directory (c:\users\foobar => foobar)
directory_name = os.basename(base_directory)
#List the files in base_directory
for path in os.listdir(base_directory):
old_name = base_directory + '/' + path
#If the path points to a file, rename it directory name + '.' + extension
if os.path.isfile(old_name):
new_name = base_directory + '/' + directory_name + '.' + split_name(path)[1]
if not os.path.exists(new_name):
os.rename(old_name,new_name)
else:
print("ERROR:"+new_name+" exists")
else:
#If it's a subfolder, recursively call rename files on that directory.
rename_files(old_name)
Also, I stongly suggest Learn Python The Hard Way by Zed Shaw and Dive Into Python by Mark Pilgrim
a simple windows command can solve your problem. Read HELP FOR and the try this in the Windows command line:
for /d %a in (*) do #for %b in (%a\*) do #ren %a\%b %a%~xb
let's analyze it
the first for will iterate over all (*) the directories /d and for each found, passed in %a the second for will iterate over all the files it contains (%a\*) and for each file found %b it will do rename ren it %a\%b with the name of the folder it is contained in %a keeping the same extension it had %~xb.
This can be done with batch only, I publish this script only to demonstrate how easy this is in Ruby
# enumerate all subfolders of raptors
Dir.glob("raptors/**/*/") do |folder|
# remember the prefix
pre = File.basename(folder)[/.+_/]
# enumerate all files under this folder
Dir.glob("#{folder}*.*") do |file|
File.rename(file, "#{File.dirname(file)}/#{pre}#{File.basename(file)}")
end
end
There is another answer with python code, this code changed my GIS file names based on folder name very well:
Thanks #Martin Evans.

Bat file to compare files within folder A against folder B

Please can someone help with the below
I have two folders:
C:\FolderA
C:\FolderB
Folder A contains a bunch of files like an archive
Folder B contains the same bunch of files with the same name, however some data within the files may be different.
I want to write a .bat file which uses the diff command to compare all the files from folder A to the files in folder B with the corresponding name (e.g. update0001 against update 0001) and outputs the difference in "C:\Folder C" with each file difference in a separate text output. (e.g one file is called “Error update0001” and another “Error Update0005”
This is a simple way to check the files in two folders and give results.
fc /b "c:\folder a\*.*" "c:\folder b\*.*" >"c:\folder c\results.txt"

Command Prompt and batch files

I'm trying to copy a number of files from a directory. I want to include the file path from the base of this particular directory tree, however, I only have a list of the file names to go by. Is there a way to copy either:
a list of files with their directories appended to the beginning in a .txt file
a copy of the folders in the directory with copies of the files placed in their original places in the original directory.
Each file in the directory has a unique name.
I've looked all over google, but the closest I've found is xcopy, which I don't believe is capable of this.
Thanks!
For the second option you can use xcopy /s or robocopy /s. Both are great tools for this kind of job.

Resources