Link to home
Start Free TrialLog in
Avatar of speeDemon
speeDemonFlag for United States of America

asked on

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

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
Avatar of Bill Prew
Bill Prew

Can you share an example of the exported REG file, and then identify which items in it you are trying to restore.

~bp
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
Avatar of speeDemon

ASKER

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
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.
SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
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?
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
I saw that too and just place the FirstLine variable in the PuttyConfig place.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
here is my updated script
PuttyBackup-Restore.txt
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Team effort but i had to debug myself after a nights sleep