topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 4:40 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Command Line special needs for Batch file - Replace/rename/remove in filenames  (Read 6559 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
I have finally  :o managed to finish a working script to accomplish a task that I have to run several times a week.  It was something that had always been done by hand due to the specific nature of the needs.  Now that I have a working script that outputs to a dated filename, I have a few final cleanup tasks that I am still doing in notepad that could be added to the script if I knew how to do it through command line.
The output is a list of file paths as they exist relative to the directory on the server.  In their final form, they are all really hyperlinks.  To get there, I have to:
1: change all "\"  to "/".
2:  change all occurrences of "c:/1st level/2nd level/"  to "http://www."  (note that this shows the already been changed slashes)
3:  I need to remove a folder referenced in the physical local path as it doe not exist in the hyperlinks ie:  Remove all instances of "/webroot"
4:  And finally. the tricky one.  If any of the created hyperlinks ends up being a link with less than 5 "/"'s  I need to delete it as it is not a working link.
And there are always going to be several of these.  This part I have to just manually do as notepad is no help in making it any easier.

The others are all simple functions done in notepad but I cannot find the corresponding command line references to accomplish them.
I am going to try to paste in the code below as it currently exists.  The last time I tried this it would not insert due to a need for indenting?  I think?
I hope this comes through.  I am sure someone out there probably know exactly what I am missing.  
Thanks for any pointers

Code: Text [Select]
  1. @echo off
  2. for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
  3. set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%
  4. setlocal
  5. set currentLevel=0
  6. set maxLevel=%2
  7. if not defined maxLevel set maxLevel=2
  8.  
  9. :procFolder
  10. pushd %1 2>nul || exit /b
  11. if %currentLevel% lss %maxLevel% (
  12. for /d %%F in (*) do (
  13. echo %%~fF >>c:\Hyperlinks\Working-%dt%.txt
  14. set /a currentLevel+=1
  15. call :procFolder "%%F"
  16. set /a currentLevel-=1
  17. )
  18. )
  19. popd
« Last Edit: December 11, 2013, 04:11 AM by skwire, Reason: Added code tags. »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Points 1 and 2:

To replace \ with / and c:/1st level/2nd level with http://www you can use string substitution, eg.

Code: Text [Select]
  1. @echo off
  2. set URL=C:\test1\test2\test.txt
  3. set URL=%URL:\=/%
  4. set URL=%URL:C:/test1/test2=http://www%
  5. echo.%URL%

And the output:
Code: Text [Select]
  1. K:\>test2.cmd
  2. http://www/test.txt

Point 3 you could do the same (replace the string you don't want with an empty string, ie. set URL=%URL:/webroot=%).

And I think the bit below takes care of point 4:

Code: Text [Select]
  1. @echo off
  2. rem URL with less than 5 forward slashes
  3. set URL=http://not/longenuff/test.txt
  4. call :TestLen %URL%
  5.  
  6. rem URL with more than 5 forward slashes
  7. set URL=http://www/need/to/make/it/longer/test.txt
  8. call :TestLen %URL%
  9. goto :End
  10.  
  11. :TestLen
  12. for /f "usebackq delims=/ tokens=5" %%a in (`echo.%1`) do (call :LongEnuff %%a %1)
  13. goto :EOF
  14.  
  15. :LongEnuff
  16. if /i not %1 == "" echo.%2
  17. goto :EOF
  18.  
  19. :End
  20. pause

ie. Use / as a delimiter and test to see whether the fifth argument is empty or not.  Seems to work.


You may have to tweak it a bit for your setup but here goes:

Code: Text [Select]
  1. @echo off
  2.  
  3. for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
  4. set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%
  5. setlocal
  6. set currentLevel=0
  7. set maxLevel=%2
  8. if not defined maxLevel set maxLevel=2
  9.  
  10. :procFolder
  11. pushd %1 2>nul || exit /b
  12. if %currentLevel% lss %maxLevel% (
  13.   for /d %%F in (*) do (
  14.     call :ProcessFol "%%~fF"
  15.     set /a currentLevel+=1
  16.     call :procFolder "%%~F"
  17.     set /a currentLevel-=1
  18.   )
  19. )
  20. popd
  21. goto :End
  22.  
  23. :processFol
  24. set URL=%~1
  25. set URL=%URL:\=/%
  26. set URL=%URL:/webroot=%
  27. set URL=%URL:c:/1st level/2nd level=http://www%
  28. call :TestLen %URL%
  29. goto :EOF
  30.  
  31. :TestLen
  32. for /f "usebackq delims=/ tokens=5" %%a in (`echo.%1`) do (call :LongEnuff %%a %1)
  33. goto :EOF
  34.  
  35. :LongEnuff
  36. if /i not %1 == "" echo.%2 >>c:\Hyperlinks\Working-%dt%.txt
  37. goto :EOF
  38.  
  39. :End


I am going to try to paste in the code below as it currently exists.  The last time I tried this it would not insert due to a need for indenting?  I think?

When you insert your code into a post, use the drop down list in the editor, then paste your code between the tags.

2013-12-11 20_31_24-Modify message - DonationCoder.com - Pale Moon.pngCommand Line special needs for Batch file - Replace/rename/remove in filenames

« Last Edit: December 11, 2013, 04:40 AM by 4wd »

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
DOUBLE DUH!  4wd.  Thanks for pointing me back here.  I had lost track of this and never did get to see your helps.

MANY GRATEFUL THANKS!  I have sent you a few PM's I hope you are getting them

I am going to transfer the scripts right now and test it on my last run to see if all works out.
Where Shades was asking me to use pen and paper ,.

even if I did, I don't think my drawing could be understood either.  I cannot draw nearly as well as I can type and even THAT is not so good.

I really thought my description using dir1/dir2/dir3/dir4/ etc was about as good as it gets but I am sure it depends on HOW each person "sees" a problem in their own mind.

I "see" it as an actual physical structure where I need to remove a  4th floor from a 6 story building.  Plus replace the stairs with an escalator  :)
Well something like that.



questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
This is almost over! But. As all works in progress there is always more..
« Reply #3 on: February 14, 2014, 02:37 PM »
 :-[
OK, Many thanks to everyone who posted.  From those and from pieces here and there I finally got this thing to fly.
The finish touches are probably easy.  But I have been out of sort for the past week or so and this is still eluding me

The final mash-up of all the codes resulted in this one batch file.  It does work!  For that I am thankful.
There are only two small places where I need to accomplish the finishing touches to a one-click solution.  I have tried a few things but in most cases I seem to be ending up will errors in creation for some reason.

Below is the working code with the two sections where I am trying to accomplish the tasks I wrote in at those points in ALL CAPS after a ##

Code: Text [Select]
  1.  
@echo off
:: timestamp YYYY-MM-DD_HH-MM-SS
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%
setlocal
set currentLevel=0
set maxLevel=%2
if not defined maxLevel set maxLevel=2

:procFolder
pushd %1 2>nul || exit /b
if %currentLevel% lss %maxLevel% (
  for /d %%F in (*) do (
    echo %%~fF >>c:\activelink\activelink-%dt%.txt
    set /a currentLevel+=1
    call :procFolder "%%F"
    set /a currentLevel-=1
  )
)
popd

:: start next

Set "InputFile=c:\activelink\activelink-%dt%.txt"
Set "OutputFile=c:\active-link\active-link-%dt%.txt"

setLocal EnableDelayedExpansion > "%OutputFile%"

for /f "usebackq tokens=* delims= " %%a in ("%InputFile%") do (
set s=%%a
>> "%OutputFile%" echo.!s:~59!
)

## AT THIS POINT I WOULD LIKE TO ADD 15 SPACES BEFORE CONTENTS OF THE LINE. 
LINES WITHOUT A "\" AT THIS POINT ARE THE NAME OF THE OWNER OF THE FOLLOWING LINES )

(for /f "delims=" %%L in (c:\active-link\active-link-%dt%.txt) do @echo http://www.mysite.com/%

%L)> c:\active-link\active-link-A-%dt%.txt

"C:\fnr.exe" --cl --dir "C:\active-link" --fileMask "active-link-A-%dt%.txt" --excludeFileMask "*.dll,

*.exe" --find "\\" --replace "/"
Code: Text [Select]
  1.  

This is probably an ESS  (Extremely Sloppy Script) but it gets the job done.
Any suggestions on shortening appreciated.  The FNR.EXE utility was very helpful and 4WD's coding I borrowed from anywhere I could get it to work on my directory structure.
The only two tasks left could really be done with a text editor (And that is what I am doing at this time) but.. as always
Repetitive tasks are best left to the computer. ;)

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
You're not quite there yet with the code tags :)

You're supposed to put your code between the code and /code - you've put

code
/code
batch file
code
/code


## AT THIS POINT I WOULD LIKE TO ADD 15 SPACES BEFORE CONTENTS OF THE LINE. 
LINES WITHOUT A "\" AT THIS POINT ARE THE NAME OF THE OWNER OF THE FOLLOWING LINES )

Do you want something like this:

Code: Text [Select]
  1. John
  2. http://www.john.com/.....
  3. http://www.john2.com/.....
  4.                Fred
  5. http://www.fred.org/....
  6.                Julie
  7. http://www.julie.me/

If so I would think that the same method as in my post above could be used:

Code: Text [Select]
  1. @echo off
  2. rem URL with less than 5 forward slashes
  3. set URL=http://not/longenuff/test.txt
  4. call :TestLen %URL%
  5.  
  6. rem URL with more than 5 forward slashes
  7. set URL=http://www/need/to/make/it/longer/test.txt
  8. call :TestLen %URL%
  9. goto :End
  10.  
  11. :TestLen
  12. for /f "usebackq delims=/ tokens=5" %%a in (`echo.%1`) do (call :LongEnuff %%a %1)
  13. goto :EOF
  14.  
  15. :LongEnuff
  16. if /i not %1 == "" echo.%2
  17. goto :EOF
  18.  
  19. :End
  20. pause

ie. Use / as a delimiter and test to see whether the fifth argument is empty or not.  Seems to work.

Except this time your delimiter is \ and you only want to test the second argument, if it's empty then there was no \.

So the changed bits:

Code: Text [Select]
  1. for /f "usebackq delims=\ tokens=2" %%a in (`echo.%1`) do (call :NoSlash %%a %1)
  2.  
  3. ...
  4.  
  5. :NoSlash
  6. if /i %1 == "" echo.               %2
  7. goto :EOF

Adjust rest as needed.

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Seems there are days when things just never go right. :huh:  Every time I get something to where I think it is right, hit Post and with for an Instant Retract That button  :-[

Yes, you are exactly right (oddly, you almost got the name right too :) ) It actually should end up more like:
1.               John

2.http://www.MYSITE.CO.../john/johnsfirstsite
3.http://www.MYSITE.CO...john/johnssecondsite

4.               Fred
5.http://www.MYSITE.CO.../Fred/fredsfirstsite

6.               Julie
7.http://www.MYSITE.CO...ulie/juliesfirstsite

and so on.  Each name has at least one sub-site  some have as many as 10.
The "MYSITE.COM" just acts as a "host" for all the user's sub-sites.
Probably are better ways to do it but this is the way it was so I left the layout alone as it works.


The part of the code that reduce it to just the everything after the first 59 characters ends up with the name on the first line with the paths for that name's sites on each following line, then another name line.  That almost got me there.  At this point I should have done the deed with the extra spaces for a division as you show.

Then I found your trick for replacing a "\" with the "/"  by using using a double "\\" to simulate "\"  and replace it with "/".  I stuck that in and was so happy to see it worked that I almost did not notice it got to the end and started over again in the resulting text file.  I also ran into trouble trying to convert the Text to a DOC so that the links would actually work.  I guess even an RTF would have been enough.  I think I got lost in the middle parts where the inputs need to only be there until the output.  Too bad I cant use the input filename AS the output filename to save so many middle steps.

Somewhere in all this, I got into a loop (Surprise!) that ended up with FNR running for almost 3 minutes until finally stopping.  Not sure why it ran out of steam but it did.   The results though ended up doubled so I figured I was looping through at least twice.

By then I was ready to hang up my batch-hat and just edit the rest.
The trick there would be probably another length test.  the lines with just a name are always shorter by far than those after the name that have a full path
Easier to test for length than test for the "/" character.  But my eyes were getting blurry (again)  Allergies/Flu  who knows.  Either way, to  hard to see.

I was still trying to figure out the logic, gave up, posted what I had and went home to bed.
Thanks for your input as always.  It would help if I was even half as good at it as you are    ;) but I will try your latest in the morning and see how it goes.

Also  DUH  I will NOT screw up my next code post, I saw those two start-end boxes in the prview but I figured they would merge in the final.
I See now though.
Thanks

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Also  DUH  I will NOT screw up my next code post, ...

Yeah sure, I believe you ...  ::)

;D