how to remove a few lines from a Unicode registry file using batch commands in Windows? - unicode

I have a program who's generating some data in registry.
I save it with "reg export HKCU\Software\ProgramName\Data data.reg" (Unicode format).
I need to take it to other computer and import it there so the program from that computer could use the data.
But I have to remove some text lines from data.reg. The text lines are easy to find because they contain some strings (for example paths to exe and dlls, specific program settings like "name1=value1", "name2=value2",...).
Now I'm doing this manually (using Wordpad) every few days but maybe there is another way...
Oh and I can't install other programs on these computers (the access is restricted) so I have to use batch/cmd files.
What I tried so far:
- redirecting the export to "con" but is visual only not in a variable;
- using "for /F ..." but this works only with ANSI and removes blank lines.
The lines must be removed before importing because the settings of the program from the first computer must not be loaded into the registry keys of the program from the second computer.
Can somebody please help me...?
Thank you.

Use this code to loop the file contents line by line
FOR /F %z IN (yourfile.reg) DO ...
Then use conditionals to determine if this line is one you want to keep
IF (%z)==[put your key string here]
If so (or if not), then write that variable to the target file
#echo %z >> output.reg
You can replace the filenames with command line arguments, use %1, %2, etc.
All together, then:
FOR /F %z IN (%1) DO IF NOT (%z)==[skip this line] #echo %z >> %2
Since you didn't give some critical details, I am making some assumptions here. Further reading can be found at this excellent resource: http://www.robvanderwoude.com/batchfiles.php

An example would be nice, but could you use reg delete to delete the keys/values you want after importing your .reg file?

Related

Xcopy transfer not working

I am using the xcopy command to copy files from one directory to another.
I am saving in the text editor as a .bat file and then running as admin.
Here is the command.
xcopy C:\Users\Public\Public Documents\Saved Spectral Files Z:\Contamination_Control\MOC Measurements\MOC Plates Data\Perkin Elmer Data Back-Up\ /d /f /p
The problem is it doesn't work. Not even a notification that anything has happened.
Any help would be appreciated.
Thanks,
N
You have to insert quotation marks around the two file paths, because without them, in my experience with xcopy, doesn't recognize the files. Sidenote: the same applies to the copy function.
As an add-on to the previous answer:
The problem is your empty spaces " " within the paths. For the command to know where one path starts and ends, always use the quotation marks.

Move batch file on specific date

I'm new to batch. I want to move a file hello.bat to the startup folder, but only on a specific date.
How do I insert "if then" statements (e.g. If "date" Then "execution")?
Furthermore, how do I move a file?
I've tried this using what I've gathered from Google:
If %date% NEQ 2015/12/25 goto asdf
move c:\Users\USERNAME\AppData\hello.bat
c:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start
Menu\Programs\Startup
:asdf
It doesn't seem to be working, however - the move part works fine, but when I insert the If statement, it doesn't compile.
Can someone offer me a solution to this problem? I feel I would learn more from an example than reading something online.
The %date% variable is different depending on your system settings. To check the format of %date%, run the following command in a cmd window echo %date%.
In my system, the date format is Day 00/00/0000. So the following would be needed (string manipulation to remove the first four characters of the date).
if "%date:~4%" NEQ "12/25/2015" goto asdf
As a side note; you can simply goto :EOF (End Of File) if you just want the script to end.

Using diff3 where filenames contain a dash (-)

I'm trying to use diff3 in this way
diff3 options... mine older yours
My problem is that I probably can't use it, since all my 3 files contain a "dash" within.
The manual mentions:
At most one of these three file names may be `-', which tells diff3 to read the standard input for that file.
so I probably have to rename filenames before running diff3.
If you know for a better solution or a workaround, please let me know about. Thank you!
At most one of these three file names may be `-', which tells diff3 to read the standard input for that file.
It does not state, that your filenames should not contain dash symbols. It simply says, that if you want to, you can put - instead of one of the names, in which case the standard input will be read instead of reading one of the files.
So, you can have as many dashes in your filenames as you like and diff3 should work just fine.
However, on Windows putting filenames in "" for escaping space characters does not work, and I failed to find a suitable workaround. However, you can automatize the process of renaming files (if the files are relatively small, this would not even be too inefficient):
#echo off
copy %1 tempfile_1.txt
copy %2 tempfile_2.txt
copy %3 tempfile_3.txt
"C:\Program Files (x86)\KDiff3\bin\diff3.exe" -E tempfile_1.txt tempfile_2.txt tempfile_3.txt
del tempfile_1.txt tempfile_2.txt tempfile_3.txt
Put this in a file like diff3.cmd, then run diff3.cmd "first file.txt" "second file.txt" "third file.txt".
P.S. Moving files would be more efficient (if they are on the same disk volume as the script, which they are not in your case), you could even move them back to where they were initially, but for some time they would not be present at their original folder.

Using wildard with DOS COPY command corrupts destination file

I don't understand the behaviour of the COPY command when using a wildcard.
I have a single text file in C:\Source called mpt*.asm and I want to copy it to C:\Dest. This is needed from a batch script, and I can't be sure of the exact name of mpt*.asm (it may be mpt001.asm for example). The destination name should be exactly mpt.asm.
If I use:
COPY C:\Source\mpt*.asm C:\Dest\mpt.asm
The file file is copied, but has a extra (0x1A) character appended to the end of the file.
If I use:
COPY C:\Source\mpt*.asm C:\Dest\mpt.asm /B
I don't get this spurious character.
If I don't use a wildcard, I don't get the spurious character either. It seems unlikely there is a bug in COPY, but this behavior seems unexpected.
Is there a way of doing this copy without resorting to using /B?
I have never seen that before, but it does append an extra arrow character for me too.
You can work around the issue using xcopy instead.
echo f| xcopy C:\Source\mpt*.asm C:\Dest\mpt.asm
If you read copy /? it says
To append files, specify a single file for destination, but multiple files
for source (using wildcards or file1+file2+file3 format).
So by using a single filename as the the dest, and using a wildcard in the source, it may interpret that as appending, which may be what the extra character is for, but as you aren't appending anything you can see it.
I'm only guessing but that may explain it.

Basic script to replace image files in multiple directories

I have a situation where we have several thousand image files that have become corrupted on our server (Windows 2008 R2 x64). I have a working image file that I want to replace the corrupt files with. The files must retain the same name and path (size, timestamps, etc do not matter).
So the basic idea would be to replace each corrupt image file with the working file.
I do not write code, only the occasional windows batch file.
Should I use VB or PowerShell (or something else) for this? What will the script look like for this?
I apologize in advance if this question is too basic for stackoverflow.
You don't really need a batch file,
try looking at the for command
e.g.
FOR /R %f in (*.jpg) DO copy newfile.jpg "%f"
This should do a recursive search and copy newfile.jpg over the jpg's it finds.
It all boils down to how you are identifying the broken jpgs.
When I dont use a wild card for example
FOR /R %f in (broken.jpg) DO copy newfile.jpg "%f"
Then newfile.jpg gets copied to every subdirectory. If I use a wildcard ( *,?) the command works as expected. Is there a way to have this commend work with a (set) that does not contain wildcards?