Reputation: 193
I've got a number of files I'm trying to sort through for my PhD research. I'm not familiar with using Batch files but I do have some coding experience.
I'm trying to move the files based on part of their filename into folders generated automatically for them in the directory they're currently in.
The files were generated as part of a series of pXRF analyses on brass objects held at a museum.
Some examples of the file structure:
etc.
Basically the file structure works like this:
The first number is my unique identifier for the object, then a hyphen, then the museums unique identifier, then a hyphen, then a number to show which analysis of the object. Finally, there may or may not be a small descriptor (i.e. BASE or LID) which helps me identify where I performed the scan.
So to break it down 01-64.1007-1-LID means Object 1 - Museum Accession Number 64.1007 - First Analysis - Performed on the lid of the object - .pdz is the file extension.
02-67.1765-2 means Object 2 - Museum Accession Number 67.1765 - Second Analysis etc.
What I want to do is move:
into a folder called:
and
into a folder called:
etc.
I've seen a few scripts which may help with this but I'm unsure how to tweak them for my purposes. If anyone can help I'd really appreciate it!
Thanks,
Mike
Upvotes: 1
Views: 4650
Reputation: 41297
Test this batch file in a folder of sample files: call it movepdz.bat
so the name doesn't clash with another executable.
@echo off
for %%a in (*.pdz) do (
echo processing "%%a"
for /f "tokens=1,2 delims=-" %%b in ("%%~nxa") do (
md "%%b-%%c" 2>nul
move "%%a" "%%b-%%c" >nul
)
)
pause
Upvotes: 1
Reputation: 26565
If you really mean a bash script, you can use something like this:
for file in *.pdz; do
dir=${echo "$file"|cut -d- -f -2}
mkdir -p "$dir"
mv "$file" "$dir"
done
The cut
command will cut out the part before the second hyphen to determine the directory name. Then we create the directory in case it is not already present. Finally simply move the file in that directory.
Update: Thanks for clarifying your environment. (I was sure the question mentioned bash before...) - If you want to use this solution you could install git which comes with a nice git-bash.
Upvotes: 0