Solved

Batch file to read all of file, search for part of a known string, then place the whole string line into a variable.

Posted on 2013-05-31
16
482 Views
Last Modified: 2013-06-09
So i have a backup and restore batch file I am working on for backing up the settings registry entry for Putty. I want to have the restore function work in a way that is easy to use. So far, i have been able to get the settings to export using the regedit /e HKCU\Software\SimonTatham.

When the export runs it exports to a shared directory and tags the export with a date string so we know when the file was last backed up...

Now for the restore, the best way I have found, but can't make work is to generate a txt file of the file contents where the reg backup is, have the batch file search this file for a known string (PuttyBackup) and have the batch file, once it finds it, cache the full name as a varibale for use later with the import script.

Any ideas? All of this is going to be done from a Windows 7 computer
0
Comment
Question by:speeDemon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
16 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39212153
Can you share an example of the exported REG file, and then identify which items in it you are trying to restore.

~bp
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39212226
If the line you are after looks like this:

"PuttyBackup"="c:\temp\file.txt"

And is in a file called save01.reg, then this will get the path and assign it to a variable.

for /F "tokens=1* delims==" %%A in ('findstr /i /c:"puttybackup" save01.reg') do set backfile=%%~B
echo %backfile%

Open in new window

~bp
0
 
LVL 1

Author Comment

by:speeDemon
ID: 39216385
almost, i think we are on the right track...

so I have a file that is always called PuttyBackupYYYYMMDD.reg

what i need to do is have the batch file import that file without hard coding the YYYYMMDD values in because they will change from time to time. So what I think i have to do is write the list of files that is in the directory to a text file, have the batch file search for a file that starts with "PuttyBackup" but save the whole result of the line (for example: PuttyBackup20130603.reg) to a variable to use during the import portion of the script...

Thanks for anyhting in advance... Here is what I have so far
PuttySettingsBackupAndRestore.txt
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:speeDemon
ID: 39216389
That last post was the current one. The filtered file list to desktop works as expected. I am just having trouble writting the first line of the file to a variable in the batch script.
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 39216417
If you want to write the very first line of a file into a var, there is a neat trick you can use:
set /p FirstLine=<"%myfile_%"

Open in new window

0
 
LVL 1

Author Comment

by:speeDemon
ID: 39216570
For some reason this is not working:

	
SET filelist=C:\Users\myuser\Desktop\file_list.txt
ECHO filelist is=%USERPROFILE%\Desktop\file_list.txt
pause
SET /p FirstLine=<%filelist%
ECHO %FirstLine%
pause

Open in new window

0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39216701
Odd, this works fine for me:

@ECHO OFF
SET filelist=%USERPROFILE%\Desktop\file_list.txt
ECHO filelist is=%USERPROFILE%\Desktop\file_list.txt
ECHO Line one>%filelist%
ECHO Line two>>%filelist%
SET /p FirstLine=<%filelist%
ECHO %FirstLine%
PAUSE

Open in new window

~bp
0
 
LVL 1

Author Comment

by:speeDemon
ID: 39216710
Not sure what was going on. Working now. Now i am having trouble with the import process through a batch file...

So the script copies over the reg settings i want to import locally (to users desktop), If i click on the files myself and merge, the merge fine, If i let the batch file try to import the file, it fails...

Any thing stick out to you?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39216737
I see this line:

regedit "%USERPROFILE%\Desktop\%puttyconfig%.reg"

but I don't see you setting the PUTTYCONFIG variable anyplace.

Your script can actually be cleaned up a bit too, we don't need a list file of the folder to the desktop to get the first line, etc, but I'm short on time until later today when I could take a shot at some simplifications.

~bp
0
 
LVL 1

Author Comment

by:speeDemon
ID: 39216738
I saw that too and just place the FirstLine variable in the PuttyConfig place.
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 400 total points
ID: 39217729
Okay, I think I have some decent changes ready.  But noticed that much of your logic relates to the following:

IF NOT EXIST "%USERPROFILE%\Desktop\PuttyConfig????????.reg"

but what do you want to do if a files DOES exist, it does nothing currently.

~bp
0
 
LVL 1

Author Comment

by:speeDemon
ID: 39217986
I took out the nest and just checked for it and deleted it if it was on the desktop currently. Then check for an existing file list. Txt and moved on.
0
 
LVL 1

Author Comment

by:speeDemon
ID: 39219907
here is my updated script
PuttyBackup-Restore.txt
0
 
LVL 1

Accepted Solution

by:
speeDemon earned 0 total points
ID: 39219916
Got it, the issue was on line 35 where i added an extra ".reg" hardcode when the variable %FristLin% had it in there already. Thank everyone for yuor help. P.S. the working script is attached.
PuttyBackupRestore.txt
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39222393
Worked this up, see if maybe it is a slightly less complex way to handle things.  Let me know if you have further questions.

@echo off
setlocal EnableExtensions
COLOR F0

REM Define backup folder location
SET BackupFolder=\\server\Share\MyShare\Documentation\Software\putty\Putty Config Backups
REM Define base of file name (date stamp will be appended to this)
SET BackupFileBase=PuttyConfig
REM Defile temprary location for reg file copy for restore
SET WorkFile=%TEMP%\%BackupFileBase%.reg
REM Define registry node we want to backup
SET RegistryNode=HKEY_CURRENT_USER\Software\SimonTatham
REM Build date stamp in YYYYMMDD format fpr backup
SET Stamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%

:Main
  REM Prompt for mode
  ECHO PuTtY Backup Utility
  ECHO ..............................
  ECHO.
  ECHO 1 - Backup Putty Settings
  ECHO 2 - Restore Putty Settings
  ECHO 3 - EXIT
  ECHO.
  ECHO ..............................
  ECHO.
  SET /P M=Type 1, 2, or 3 then press ENTER-  

  REM Jump to appropriate routine
  IF %M%==1 GOTO backup
  IF %M%==2 GOTO restore
  IF %M%==3 GOTO exit

  REM If invalid choice entered, prompt again
  GOTO Main

REM Restore
:restore
  REM Find the most recent backup and copy to TEMP folder
  SET GotFile=
  FOR /F "tokens=*" %%A IN ('DIR "%BackupFolder%\%BackupFileBase%*.reg" /a-d /b /o-n') DO (
    if not defined GotFile (
      set GotFile=Y
      copy "%%~A" "%WorkFile%" /Y
    )
  )
  REM Load into registry
  regedit "%WorkFile%"
  REM Delete TEMP reg file
  DEL "%WorkFile%"
  GOTO EXIT

REM Backup
:backup
  REM Acrhive any older backup files
  IF EXIST "%BackupFolder%\%BackupFileBase%*.reg" move "%BackupFolder%\%BackupFileBase%*.reg" "%BackupFolder%\Old Versions\"
  REM Export settings from registry
  regedit /e "%BackupFolder%\%BackupFileBase%%Stamp%.reg" %RegistryNode%
  GOTO EXIT

:exit
  exit /b

Open in new window

~bp
0
 
LVL 1

Author Closing Comment

by:speeDemon
ID: 39232741
Team effort but i had to debug myself after a nights sleep
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question