Merge/rename folders with a batch

Microsoft / Dos
February 12, 2010 at 06:18:14
Specs: Windows XP
I'm using a batch to copy a folder which
contains subfolders, which on their turn
contain subfolders aswell.
The batch promts for the first folder and the
location where to save it.
What I want is that after the copying is done,
the last-mentioned subfolders get renamed,
sometimes 2 of these need to be merged to
one and renamed.
I found some stuff here already, but the names
of the first subfolders are variable, only the last
subfolders have constant names, though
sometimes some of these might not be there.
Can anyone help me with this?

See More: Merge/rename folders with a batch

Report •

#1
February 12, 2010 at 10:19:27
might need to try to clarify just a bit, explain in greater detail and give examples of the dir structure (just use short names to represent subdirs).

Report •

#2
February 13, 2010 at 07:33:58
You indeed need to specify more info, you need to specify the rules ... HOW do they change, give us info, or examples ... we need to understand WHY you need to change them.

Report •

#3
February 15, 2010 at 02:30:07
OK, the folders that get copied are prompted, so I have that value
stored already. Next I have subfolders which always have variable
names.
In these folders are up to 3 subfolders named 'Lists',
'Docs' and 'Images'. All three contain PDF files, which won't get changed.
Now what I want is that the 'Docs' and
'Images' folders get merged together and renamed to 'Others'. It is also
possible for the 'Docs' and/or the 'Images' folders not to exist. If
one of both does not exist, the other still needs to be renamed to
'Others'.
It might also be possible to move the files from the 'Image' and/or the 'Docs' folder into a new folder 'Others', after which the 'Image' and/or 'Docs' folders will be removed.
In the end, I need to have the prompted folder containing various subfolders with variable names, which contain up to 2 subfolders: 'List' and/or 'Others'.
Is this enough info?

Report •

Related Solutions

#4
February 15, 2010 at 07:05:37
> In these folders are up to 3 subfolders named 'Lists',
> 'Docs' and 'Images'. All three contain PDF files, which
> won't get changed.
> Now what I want is that the 'Docs' and
> 'Images' folders get merged together and renamed to
> 'Others'. It is also
> possible for the 'Docs' and/or the 'Images' folders not
> to exist. If
> one of both does not exist, the other still needs to be
> renamed to
> 'Others'.
> It might also be possible to move the files from the
> 'Image' and/or the 'Docs' folder into a new folder
> 'Others', after which the 'Image' and/or 'Docs' folders
> will be removed.

@echo off

if not exist Others mkdir Others

REM Existing named files in Others are NOT overwritten
if exist Docs\*.pdf move /-Y Docs\*.pdf Others
if exist Images\*.pdf move /-Y Images\*.pdf Others

REM Directories only get deleted if no files in them - Note :will fail if directories would be in there
if NOT exist Docs\*.* rmdir Docs
if NOT exist Images\*.* rmdir Images


Report •

#5
February 15, 2010 at 07:09:22
> In the end, I need to have the prompted folder
> containing various subfolders with variable names,
> which contain up to 2 subfolders: 'List' and/or
> 'Others'.
> Is this enough info?
>

I'm not sure I understand this, but this is my attempt:

if not exist %inputvariable% mkdir %inputvariable%
if not exist %inputvariable%\List mkdir %inputvariable%\List
if not exist %inputvariable%\Others mkdir %inputvariable%\Others


Report •

#6
February 15, 2010 at 11:24:23
A thousand ways to do the same thing:
for %%a in ("%inputvariable%\Docs" "%inputvariable%\Images") do (xcopy /e /i %%a "%inputvariable%\Others" && rd /s /q %%a)


Report •

#7
February 15, 2010 at 23:59:08
Non of the above will work, I know that for sure. See, I have the
prompted folder, which has a name containing a date, this folder
contains subfolders. These subfolders have variable names,
containing letters and numbers. These folders are NOT prompted
and thus not directly recognized by my batch file. The use of a wildcard doesn't work either, as I tested that...

Report •

#8
February 16, 2010 at 01:08:29
WHEW!
like being in school again...
you have definitely posed the challenge of the day.
I tink tis problem needs to be chunked into small bits...
you have folders "lists", "doc" and "images" somewhere, poss. in multiple locations, down the "tree" from your "benchmark" (ie, "known root dir").
"doc" and "images" > "other"
I don't see where "list" needs any action as you did not say it needed to be moved... so, try this out (just a prototype!)
(leaving echo on for debugging purposes...)
::@echo off
pushd root
for /f "tokens=* delims=" %%a in ('dir /b /s /ad images') do (
cd %%a
cd ..
:: informational: where am i now???
cd
pause
md others 2>nul
pause
move images\*.pdf .\others
popd
pushd root
for /f "tokens=* delims=" %%a in ('dir /b /s /ad docs') do (
cd %%a
cd ..
:: informational: where am i now???
cd
pause
md others 2>nul
pause
move docs\*.pdf .\others
popd

Report •

#9
February 16, 2010 at 03:46:48
These folders are NOT prompted and thus not directly recognized by my batch file.
I apparently don't follow. Do you mean "%inputvariable%" has other subdirectories, or do you mean "%inputvariable%\docs" and "%inputvariable%\images" have subdirectories? If it's the former, we focused on the part not spelled out in MOVE's/XCOPY's documentation. If it's the latter, my version does account for subdirectories.

Report •

#10
February 16, 2010 at 07:18:26
@nbrane:
I assume I must replace 'root' with my prompted location?
I will try it anyway.
EDIT: I tried it, but it didn't work. It crashed at the first for command.

@Razor2.3:
The %inputvariable% is a folder that has been prompted, this
is stored for use in the batch. The batch can then be able to
do anything in that folder once it has been copied. This folder
has subdirectories with names that are variable, they can be
B20090831 or A20100126 or anything else with a letter and a
date. These folder-names are completely variable, always
different, so I think a wildcard or equivalent needs to be used
here, if possible.


Report •

#11
February 16, 2010 at 07:49:20
I think a wildcard or equivalent needs to be used
here, if possible.

You'd think, but according to what you've told us, you'd be wrong. The directory name would be enough.

Report •

#12
February 16, 2010 at 11:33:10
> Non of the above will work, I know that for sure. See, I have the
> prompted folder, which has a name containing a date, this
> folder
> contains subfolders. These subfolders have variable names,
> containing letters and numbers. These folders are NOT
> prompted
> and thus not directly recognized by my batch file. The use of
> a wildcard doesn't work either, as I tested that...
>

How do they relate then ... post the dir-strcuture please


Report •

#13
February 16, 2010 at 13:09:31
yes, "root" is supposed to be replaced with your "known/prompted" directory location,
for simplicity, i just call it dir1:
here's what i tried to do:
pushd dir1
:: this puts you "above" or upstream from the targets
for /f "tokens=* delims=" %%a in ('dir /b /s /ad images') do (
:: this finds any subdirs downstream named "images"
cd %%a
:: this moves to that directory, whereever it may be
cd ..
:: this moves one directory up from "images" which is
:: supposed to be the letters+numbers "unknown" dir.
cd
pause
:: that's why i put this CD here, so you could maybe report
:: where the script wound up. (hint)
:: it should look like: dir1\A20100126\
:: or even dir1\A20100126\B20090831\
:: (which maybe you don't want)
md others 2>nul
:: this creates "others" off of whereever you are
:: like: dir1\A20100126\others
move images\*.pdf .\others
:: this does the move
:: should look something like:
::move dir1\A20100126\images\*.pdf dir1\A20100126\others
:: (i might should have put the implied dot before "images"):
:: move .\images\*.pdf .\others


Report •

#14
February 16, 2010 at 23:51:46
@Razor2.3:
The problem is that the directory name is unknown, it could
be any name as well.

@tvc and others:
root (is prompted and stored in the batch, is not the actual
root, but all folders up to this one are prompted)
|
+ subdirectory (the one with the unknown name)
| |
| + images-folder, will be merged/renamed to others-folder
| + docs-folder, will be merged/renamed to others-folder
| + lists-folder
| + others-folder comes here
|
+ other subdirectory (has the same content as above and an
unknown name too)

And it goes on like that about 6 times at max.


Report •

#15
February 24, 2010 at 07:03:53
Is there someone who can help me with my problem?

Report •

#16
March 3, 2010 at 05:42:10
I'm quiting this. I read in an other topic that DOS is unable to
track down the subfolders of those folders with variable names.
I know a scripting language that might help me with this.
Also, thanks for all the help and support.

Report •

Ask Question