Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Append name of parent folders to the names of the files  (Read 11508 times)

0 Members and 1 Guest are viewing this topic.

billrich

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #30 on: March 16, 2009, 04:38:00 AM »
Dias de verano  wrote:

"If the original filename had any spaces in it it would fail."


Bill writes:

We solved that problem by using "" quotes around the old file name and the new file name. For example:  ren "%%a"  "2009.January.%%a"
 output:
08/04/2004  07:00 AM       81,433 2009.January.Red moon desert.jpg
Thanks for your help.


C:\>type renamejpg.bat
@echo off

Rem  run this batch file with 3 command line variables
REM  renamejpg.bat  c:\new      jpg  2009.January

REM  renamejpg.bat  destination extension Portion of file name

setlocal

REM Copy *.%2  %1
Rem Must be in the directory where files are originally

cd  %1

for /f "tokens=*" %%a in ('dir /b *.%2') do ren "%%a"  "%3.%%a"

C:\>renamejpg.bat  c:\new      jpg  2009.January


C:\>cd new
C:\new>dir *.jpg
 
 Volume Serial Number is B8F2-4B99

 Directory of C:\new


09/24/2008  09:07 PM            22,033 2009.January.piper.jpg
08/04/2004  07:00 AM            57,925 2009.January.Purple flower.jpg
08/04/2004  07:00 AM            44,190 2009.January.Radiance.jpg
08/04/2004  07:00 AM            81,433 2009.January.Red moon desert.jpg
12/03/2008  07:07 PM            58,551 2009.January.redflower.jpg
05/08/2008  07:47 PM            63,784 2009.January.Slide2.jpg
05/08/2008  07:48 PM            41,097 2009.January.Slide4.jpg
05/09/2008  07:10 AM            42,105 2009.January.Slide6.jpg
05/09/2008  07:11 AM            51,122 2009.January.Slide8.jpg
05/09/2008  07:12 AM            55,696 2009.January.Slide9.jpg
08/04/2004  07:00 AM            73,159 2009.January.Tulips.jpg
08/04/2004  07:00 AM            37,246 2009.January.Wind.jpg
08/22/2008  10:17 AM             8,165 2009.January.wolf3.jpg
              31 File(s)      2,169,842 bytes
             

C:\new>
« Last Edit: March 16, 2009, 03:09:25 PM by billrich »

Dias de verano

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #31 on: March 16, 2009, 04:44:17 AM »
suppose the path isn't 2009\January?

Dias de verano

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #32 on: March 16, 2009, 05:08:12 AM »
Regarding spaces, I was thinking about the lack of a delims block, but the tokens=* fixed that. Plus the quotes of course.

Without wanting to seem vain, I did already post a working example which answers the OP's request, (to copy a file to a folder and automatically name the copy with the elements of its previous path). Billrich still hasn't done that.

And the OP wants to copy the file to a folder and give it a new name, not rename it in place. (read the original post).



BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Append name of parent folders to the names of the files
« Reply #33 on: March 16, 2009, 09:02:48 AM »
Additionally it would be far easier to read if somebody would use code tags...
I was trying to dereference Null Pointers before it was cool.

billrich

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #34 on: March 16, 2009, 03:24:46 PM »
Bc,


Information that is not needed  by BC or Dias de verano  might be be useful to new people.  I will erase almost all my posts.   If any of the remaining posts harm the Board, feel free to erase the remaining posts also.

Good Luck with helping people.  There is not much feedback.  Maybe no feedback means the board helped somebody?
« Last Edit: March 16, 2009, 07:11:45 PM by billrich »

Dias de verano

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #35 on: March 16, 2009, 03:28:55 PM »
billrich, why are you wasting all our time?

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Append name of parent folders to the names of the files
« Reply #36 on: March 16, 2009, 03:30:04 PM »
Quote
Bc,
From for /? at command prompt.


I fail to see what that has to do with my post, but alright.

I was trying to dereference Null Pointers before it was cool.

Dias de verano

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #37 on: March 16, 2009, 04:38:29 PM »
This thread has really degenerated.

billrich

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #38 on: March 16, 2009, 06:29:21 PM »
Bc,

I'm trying to learn how to use the for loop.  I have not seen any for loops from BC.  I appreciate your help.

BC had suggested more tags and information about the for loop so I suggested the for /? at command prompt. 

I try to be nice and I still receive many negative comments.  My "for loop" was an excellent solution to the the copy and rename file names problem.

What should I do to remain on this board?   

What does BC, Carbon and Dias de verano want me do?   

I feel you do not want to help me.   Are my posts really that bad?

Why is Dias de verano  so cruel to new people?

Do you want me to leave?



From for /? at command prompt.

Runs a specified command for each file in a set of files.

Dias de verano

  • Guest
Re: Append name of parent folders to the names of the files
« Reply #39 on: March 17, 2009, 12:06:18 AM »
Quote
I try to be nice

You hijacked another person's thread and made it about "you" and made many trollish posts.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Append name of parent folders to the names of the files
« Reply #40 on: March 17, 2009, 05:34:41 AM »
Code: [Select]
[quote author=billrich link=topic=78827.msg521043#msg521043 date=1237249761]
BC had suggested more tags and information about the for loop so I suggested the for /? at command prompt. 
[/quote]

That had absolutely nothing to do with what I meant.


I meant, instead of all this:

[quote author=billrich link=topic=78827.msg520596#msg520596 date=1237199880]
C:\>type renamejpg.bat
@echo off

Rem  run this batch file with 3 command line variables
REM  renamejpg.bat  c:\new      jpg  2009.January

REM  renamejpg.bat  destination extension Portion of file name

setlocal

REM Copy *.%2  %1
Rem Must be in the directory where files are originally

cd  %1

for /f "tokens=*" %%a in ('dir /b *.%2') do ren "%%a"  "%3.%%a"

C:\>renamejpg.bat  c:\new      jpg  2009.January


C:\>cd new
C:\new>dir *.jpg
 
 Volume Serial Number is B8F2-4B99

 Directory of C:\new


09/24/2008  09:07 PM            22,033 2009.January.piper.jpg
[b]08/04/2004  07:00 AM            57,925 2009.January.Purple flower.jpg[/b]
08/04/2004  07:00 AM            44,190 2009.January.Radiance.jpg
[b]08/04/2004  07:00 AM            81,433 2009.January.Red moon desert.jpg[/b]
12/03/2008  07:07 PM            58,551 2009.January.redflower.jpg
05/08/2008  07:47 PM            63,784 2009.January.Slide2.jpg
05/08/2008  07:48 PM            41,097 2009.January.Slide4.jpg
05/09/2008  07:10 AM            42,105 2009.January.Slide6.jpg
05/09/2008  07:11 AM            51,122 2009.January.Slide8.jpg
05/09/2008  07:12 AM            55,696 2009.January.Slide9.jpg
08/04/2004  07:00 AM            73,159 2009.January.Tulips.jpg
08/04/2004  07:00 AM            37,246 2009.January.Wind.jpg
08/22/2008  10:17 AM             8,165 2009.January.wolf3.jpg
              31 File(s)      2,169,842 bytes
             

C:\new>
[/quote]

use this:

[code]
@echo off

Rem  run this batch file with 3 command line variables
REM  renamejpg.bat  c:\new      jpg  2009.January

REM  renamejpg.bat  destination extension Portion of file name

setlocal

REM Copy *.%2  %1
Rem Must be in the directory where files are originally

cd  %1

for /f "tokens=*" %%a in ('dir /b *.%2') do ren "%%a"  "%3.%%a"

IE a code tag; the forum has a lot of useful bbcode tags that work wonderfully.


Quote

I'm trying to learn how to use the for loop.  I have not seen any for loops from BC.  I appreciate your help.


isn't two Do...Loops Enough?  ;D



regardless of which, I'm not particularly good with the new command line extensions; I stopped using batch programming extensively before I switched from windows 3.1, so I haven't really researched all the new stuff that can be done with it. I can use For quite well for it's original specs, but I haven't had time to sufficiently learn the new nuances added with CMD.

I have however learned not to say stuff is impossible in batch, because the moment I say that, and provide a VBScript solution Dias has a much shorter Batch program that does the same thing  8)... and even with VBScript I keep trying to use left$ Right$ Mid$ etc, when in fact VBScript has no notion of any type other then variant, and thus there is no $ suffix on any string manipulation functions. I still type them instinctively so I have to always check over my VBScript to make sure that I didn't let some money sneak in  ;D


The fact of the matter is- this thread was solved when Dias posted his batch. your post could only suffice to confuse the OP since- as explained - it merely illustrated the REN command.[/code]
I was trying to dereference Null Pointers before it was cool.