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
476 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 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
I saw that too and just place the FirstLine variable in the PuttyConfig place.
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
here is my updated script
PuttyBackup-Restore.txt
0
 
LVL 1

Accepted Solution

by:
speeDemon earned 0 total points
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
Team effort but i had to debug myself after a nights sleep
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

743 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

16 Experts available now in Live!

Get 1:1 Help Now