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
479 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 52

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 52

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
 
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 68

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 52

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 52

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 52

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 52

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
moving files on usb stick 3 45
Laptop Using too much memory when no apps loaded 7 42
Batch/VBScript : Disable Windows tasks 7 23
batch file problem 5 21
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

914 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

19 Experts available now in Live!

Get 1:1 Help Now