ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Command Line special needs for Batch file - Replace/rename/remove in filenames

(1/2) > >>

questorfla:
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 ---@echo offfor /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%aset dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%setlocalset currentLevel=0set maxLevel=%2if not defined maxLevel set maxLevel=2  :procFolderpushd %1 2>nul || exit /bif %currentLevel% lss %maxLevel% (for /d %%F in (*) do (echo %%~fF >>c:\Hyperlinks\Working-%dt%.txt set /a currentLevel+=1call :procFolder "%%F"set /a currentLevel-=1))popd

4wd:
Points 1 and 2:

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


--- Code: Text ---@echo offset URL=C:\test1\test2\test.txtset URL=%URL:\=/%set URL=%URL:C:/test1/test2=http://www%echo.%URL%
And the output:

--- Code: Text ---K:\>test2.cmdhttp://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 ---@echo offrem URL with less than 5 forward slashesset URL=http://not/longenuff/test.txtcall :TestLen %URL% rem URL with more than 5 forward slashesset URL=http://www/need/to/make/it/longer/test.txtcall :TestLen %URL%goto :End :TestLenfor /f "usebackq delims=/ tokens=5" %%a in (`echo.%1`) do (call :LongEnuff %%a %1)goto :EOF :LongEnuffif /i not %1 == "" echo.%2goto :EOF :Endpause
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 ---@echo off for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%aset dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%setlocalset currentLevel=0set maxLevel=%2if not defined maxLevel set maxLevel=2 :procFolderpushd %1 2>nul || exit /bif %currentLevel% lss %maxLevel% (  for /d %%F in (*) do (    call :ProcessFol "%%~fF"    set /a currentLevel+=1    call :procFolder "%%~F"    set /a currentLevel-=1  ))popdgoto :End :processFolset URL=%~1set URL=%URL:\=/%set URL=%URL:/webroot=%set URL=%URL:c:/1st level/2nd level=http://www%call :TestLen %URL%goto :EOF :TestLenfor /f "usebackq delims=/ tokens=5" %%a in (`echo.%1`) do (call :LongEnuff %%a %1)goto :EOF :LongEnuffif /i not %1 == "" echo.%2 >>c:\Hyperlinks\Working-%dt%.txtgoto :EOF :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?-questorfla (December 10, 2013, 10:53 PM)
--- End quote ---

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

Command Line special needs for Batch file - Replace/rename/remove in filenames

questorfla:
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:
 :-[
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 --- @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 --- 
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:
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 )-questorfla (February 14, 2014, 02:37 PM)
--- End quote ---

Do you want something like this:


--- Code: Text ---Johnhttp://www.john.com/.....http://www.john2.com/.....               Fredhttp://www.fred.org/....               Juliehttp://www.julie.me/
If so I would think that the same method as in my post above could be used:


--- Code: Text ---@echo offrem URL with less than 5 forward slashesset URL=http://not/longenuff/test.txtcall :TestLen %URL% rem URL with more than 5 forward slashesset URL=http://www/need/to/make/it/longer/test.txtcall :TestLen %URL%goto :End :TestLenfor /f "usebackq delims=/ tokens=5" %%a in (`echo.%1`) do (call :LongEnuff %%a %1)goto :EOF :LongEnuffif /i not %1 == "" echo.%2goto :EOF :Endpause
ie. Use / as a delimiter and test to see whether the fifth argument is empty or not.  Seems to work.-4wd (December 11, 2013, 12:56 AM)
--- End quote ---

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 ---for /f "usebackq delims=\ tokens=2" %%a in (`echo.%1`) do (call :NoSlash %%a %1) ... :NoSlashif /i %1 == "" echo.               %2goto :EOF
Adjust rest as needed.

Navigation

[0] Message Index

[#] Next page

Go to full version