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
480 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
  • 9
  • 6
16 Comments
 
LVL 53

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 53

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 53

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 53

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 53

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 53

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now