I am using gdalsrsinfo to extract the projection details of vector files in multiple folders using the code mentioned below:
for /R .\ %%f IN ("*.tab") DO gdalsrsinfo -o wkt "%%f" >>U:\srsinfo.csv
Though the output file has been created with the projection details, it's not listing the filenames. Output e.g.:
GEOGCS["unnamed",DATUM["North_American_Datum_1983",SPHEROID["GRS 80",6378137,298.257222101],TOWGS84[0,0,0,-0,-0,-0,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]
GEOGCS["unnamed",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563],TOWGS84[0,0,0,-0,-0,-0,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]
Without filenames, I can't use this code. Is it possible to extract the respective filenames?
Related
I am trying to extract a .pst file from a windows backup. In order to do this I need to copy each ‘partial’ file from the backup zips and then combine them together to make the one file. I have a command that will copy them out and combine them from this post but the problem I have is that cmd is not doing it in numerical order, therefore the file is not complete. I am using this script to put the files in order:
Echo y | for /F "tokens=*" %A in (filenamesinorder.txt) do copy /b %A “c:\pstcombiner\combined.pst”
But all this does is copy each individual file and overwrites it. I get that that’s what the command does but I need it to combine all the files into one. What am I doing wrong?
Form the Microsoft documentation for the copy command:
To append files, specify a single file for destination, but multiple files for source (use wildcard characters or file1+file2+file3 format).
You’ll need to construct the source text for the copy in your for loop and do the copy after; I’ll see if I can provide an example when I am at my Windows system.
Instead of concatenating, you could try merging using the type command:
create an empty target file
copy nul target.ext > nul
then loop the type command to merge the files to the end of the target file
type fileN.ext>>target.ext
where fileN.ext is file 1, 2,3 ... n
This question already has answers here:
How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?
(6 answers)
Closed 4 years ago.
I would like to use a .bat file to unzip a ZIP compressed archive file if possible at all. Nothing fancy, I just want to extract the entire archive file to the same location, i.e. download a .zip file to desktop and want to extract it next to desktop with the same name.
I tried this, but with no success.
for /R "C:\Users\Desktop\test.zip" %%I in ("*.zip") do(
"%ProgramFiles(x86)%\7-zip\7z.exe" x - y -o"%%~dpnI" "%%~fI"
)
exit
You shouldn't need the loop. Depending on whether you want to extract the directory structure contained within the archive or just extract everything to a single directory, you would use:
7z e C:\Users\Desktop\test.zip -o C:\Users\Desktop\test
or
7z x C:\Users\Desktop\test.zip -o C:\Users\Desktop\test
See https://sevenzip.osdn.jp/chm/cmdline/commands/index.htm for a list of commands and drill down as needed for the various options.
You should not need a for loop in your batch file, unless you intend to only extract files based on a list of patterns.
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.
I am trying to convert a large number of .jpg files to the medical image format .dcm. There are many folders (with no subfolders) within a directory called C:\dicom. Each of these contains a patient specific .jpg called "REF.jpg" that needs to be converted to a file called "request.dcm" by using a small utility called img2dcm located in C:.
Each folder also contains a patient specific file called "IMG.dcm" used as a template for the conversion.Patient specific metadata is inserted from the template into the newly created request.dcm file.
For an individual folder called "foldername" containing the "REF.jpg" file, and the template file "IMG.dcm", the following command line (including the spaces) will create a usable "request.dcm" file in the same folder:
img2dcm foldername\REF.jpg foldername\request.dcm -stf foldername\IMG.dcm -k "Ser
iesDescription"=REQUEST -k "Modality"=OT -k "SeriesNumber"=200 -k "ImageNumber"=
1"
What I need to do is create a batch file to loop this command through every folder in the directory, all differently named but all containing the required files. It is crucial the newly created file be placed within its parent folder.
Any help would be greatly appreciated for what is a fairly daunting project for a someone without an IT or computing background.
#echo off
c:
cd \dicom
for /d %%i in (*) do (
img2dcm c:\dicom\%%i\REF.jpg c:\dicom\%%i\request.dcm -stf c:\dicom\%%i\IMG.dcm -k "Ser iesDescription"=REQUEST -k "Modality"=OT -k "SeriesNumber"=200 -k "ImageNumber"= 1"
)
should accomplish this.
I'd suggest you copy a portion of your data to a test subdirectory (say C:\DUMMY) and run that routine - having changed dicom throughout to dummy
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