Command Output handle emtpy Line in FOR Loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bhilde
Posts: 3
Joined: 09 Jan 2015 04:51

Command Output handle emtpy Line in FOR Loop

#1 Post by bhilde » 09 Jan 2015 05:40

##################
F:\>for /F "skip=1 tokens=1" %d in ('WMIC logicaldisk where VolumeName="BACKUP150108" get caption') do echo backupdrive="%d"

F:\>echo backupdrive="F:"
backupdrive="F:"

" \>echo backupdrive="
"ackupdrive="
##################
The command "WMIC logicaldisk where VolumeName="BACKUP150108" get caption" on the commandline returns:
Caption
F:\


Prompt:>
##################

I would like to output backupdrive="F:" only
not "ackupdrive=".
In the for statement it is possible to skip the first line (Caption) but my Problem is the last line. What must be defined in the do statement to exclude linebreak ?

thanks Bastian

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Command Output handle emtpy Line in FOR Loop

#2 Post by Yury » 09 Jan 2015 12:34

Code: Select all

for /f "skip=1" %d in ('wmic logicaldisk where VolumeName^="BACKUP150108" get caption') do @for /f %e in ("%d") do @echo backupdrive="%e"

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Command Output handle emtpy Line in FOR Loop

#3 Post by Squashman » 09 Jan 2015 12:50

Yury wrote:

Code: Select all

for /f "skip=1" %d in ('wmic logicaldisk where VolumeName^="BACKUP150108" get caption') do @for /f %e in ("%d") do @echo backupdrive="%e"

I spent a half hour this morning trying to figure that out. Never dawned on me to use a 2nd FOR command but now it makes perfect SENSE!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Command Output handle emtpy Line in FOR Loop

#4 Post by aGerman » 09 Jan 2015 13:10

Squashman wrote:I spent a half hour this morning trying to figure that out. Never dawned on me to use a 2nd FOR command but now it makes perfect SENSE!

Actually we discussed that issue several times before :wink:
E.g. viewtopic.php?f=3&t=4266

Regards
aGerman

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Command Output handle emtpy Line in FOR Loop

#5 Post by Squashman » 09 Jan 2015 13:30

My brain is crammed with too many things!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Command Output handle emtpy Line in FOR Loop

#6 Post by foxidrive » 09 Jan 2015 18:22

Squashman wrote:My brain is crammed with too many things!


yeah, I have that problem too.

When I walk down to buy milk, I'm sure that I leave a trail of things on the road that fall out, never to be remembered again.

bhilde
Posts: 3
Joined: 09 Jan 2015 04:51

Re: Command Output handle emtpy Line in FOR Loop

#7 Post by bhilde » 15 Jan 2015 04:02

Moinsen,

thanks for the answers:

1.Problem: I try to access "%a" on second subroutine - is there another way???
2.Problem: In a batch script i get a (german) syntax error "kann syntaktisch an dieser Stelle nicht verarbeitet werden." (i copy & paste the statement)

I´m going crazy of that. I suspect my Problems are already known und solved, but i am at a loose for match search-words. Sorry for that

Code: Select all

E:\>for /f "tokens=*" %a in ( 'WMIC logicaldisk where "drivetype=3 and VolumeNam
e!='BACKUP15012015'" get caption ^| findstr ":"' ) do for /f "tokens=1,2" %b in
('dir %a*_#. /ad /b') do @call echo [%b]

\*_#. /ad /b') do @call echo [%b] r C:
Datei nicht gefunden
[$Recycle.Bin]
[BACKUP]
[Config.Msi]
[Documents]
[Dokumente]
[Downloads]
[HDW30_TMP]
[KMUPDcache]
[MSOCache]
[Novell]
[PerfLogs]
[Program]
[Program]
[ProgramData]
[Programme]
[Recovery]
[savsa]
[System]
[Temp]
[test.{ED7BA470-8E54-465E-825C-99712043E01C}]
[Users]
[Windows]
[wsusconf]

\*_#. /ad /b') do @call echo [%b] r D:
Datei nicht gefunden
[$RECYCLE.BIN]
[22.02.2011]
[BR_SHARK]
[Cruzer]
[DOKUMENTE]
[DVD]
[FRAMESERVER]
[handyvideos]
[HDtmp]
[inf]
[inf2]
[INSTALL]
[kon2]
[mods_#]
[Neuer]
[Neuer]
[openldap]
[psexec]
[System]
[teamviewer]
[test]
[tidow_FB01]
[tmp]
[VPIN]
[WOL]
[xampp]


...not the correct result... replace %a to "d:"

Code: Select all


E:\>for /f "tokens=*" %a in ( 'WMIC logicaldisk where "drivetype=3 and VolumeNam
e!='BACKUP15012015'" get caption ^| findstr ":"' ) do for /f "tokens=1,2" %b in
('dir d:\*_#. /ad /b') do @call echo [%b]

E:\>for /F "tokens=1,2" %b in ('dir d:\*_#. /ad /b') do @call echo [%b]
[mods_#]

E:\>for /F "tokens=1,2" %b in ('dir d:\*_#. /ad /b') do @call echo [%b]
[mods_#]


... much better...

Code: Select all

E:\>backup_.bat
""' ) do for /f "tokens" kann syntaktisch an dieser Stelle nicht verarbeitet wer
den.

E:\>for /f "tokens=*" "' ) do for /f "tokens=1,2" a\*_#. /ad /b') do @call echo
[b]

E:\>

... and the second prob wait for me

bhilde
Posts: 3
Joined: 09 Jan 2015 04:51

Re: Command Output handle emtpy Line in FOR Loop

#8 Post by bhilde » 16 Jan 2015 06:28

new day new point of view. Here my finished script with findstr solution. Thanks for now.

Code: Select all

set robostr=/S /V /ZB /R:1 /W:1 /LOG+:Backup.log
set backupdrivevolname=BACKUP15012015
set backupmarker=*_#.
@echo off

for /f "delims=" %%A in ('WMIC logicaldisk where VolumeName^=^'%backupdrivevolname%^' get caption ^| findstr ^:') do @for /f "delims=:" %%B in ("%%A") do @set backupdrive=%%B
if X%backupdrive%X == XX (echo Kein Backuplaufwerk gefunden. Bitte pruefen ob ein Laufwerk mit der Bezeichnung: %backupdrivevolname% vorhanden ist.
)else (
echo Backuplaufwerk Bezeichnung: %backupdrivevolname% und Laufwerksbuchstaben: %backupdrive%: gefunden.
   for /f "delims=" %%A in ('WMIC logicaldisk where ^(drivetype^=3 and VolumeName^!^=^'%backupdrivevolname%^'^) get caption ^| findstr ^:') do (
      @for /f "delims=:" %%B in ("%%A") do (
         echo Durchsuche Laufwerk %%B: nach zu sichernden Ordnern
         echo die Zeichenkette %backupmarker% am Ende enthalten.
         echo Befehl: dir %%B:\%backupmarker% /ad /b /s
         for /f "tokens=1" %%C in ('dir %%B:\%backupmarker% /ad /b /s') do (
            echo Befehl: robocopy %%B:\%%C \%COMPUTERNAME%\%%B\%%C %robostr%
            robocopy %%B:\%%C \%COMPUTERNAME%\%%B\%%C %robostr%
         )
      )
   )
)

Post Reply