Delete original matching file on rename truncation - file

A friend of mine was hit with the Krypto virus. Thankfully they had Carbonite installed. So, they went ahead and restored only the affected files (.xls, .doc, jpg, scans, etc.. there were a bunch). Unfortunately they did not restore everything which would have simplified this considerably.
A restore from carbonite has been done to the directories.. now we have files that are the right ones (they all have the string: (Restored) starting at the 28th character from the end. Plus we have other valid files that krypto DID NOT encrypt.
Unfortunately, whomever did the carbonite restore put only selected files (xls, doc, ppt, etc) and left the others as is. So I can't just delete all the files. When I truncate the name... the other file exists. (THEY are frozen by the Krypto virus)... so I just want them gone. But I have to leave the other files that were not affected in place.
So lets assume I have the following files in a directory
afile1 (Restored) 11-23-2010 14.07.DOC (this filename would be truncated at
the " (" since there is already a file
called afile1.doc in the directory.
afile1.doc (This file needs to be DELETED BEFORE the truncation
the above filename so no conflict with duplicate FN
break.txt (This file has no matching file
with Restored in the name, leave alone)
cat.zip (This file has no matching file
with Restored in the name, leave alone)
fred (Restored) 01-14-14 13.28.JPG (This filename would be truncated at the " ("
but first, again the file below would have to be deleted
fred.jpg (This file needs to be deleted before the truncation
of the above filename so it won't create duplicate file
So far, I am trying to figure out... (or anything else I might have missed!)
How can I
test for existence of a matching file name where up to the " (Restored)" there is already an existing file with that name (as shown in the example above (afile1.doc & fred.jpg)
delete the already existing file (OR at least "prepend deleteme to a flie which we will have to manually delete??? then
Then truncate the last 28 characters of the filename with (Restored) in it so it is properly named and in the right place, thus preventing a duplicate file situation or worse, adding characters to the end of the valid file
I have to do this for all files under a directory too. (There are countless directories where files are stored).
I need to be left with the following files:
afile1.doc (this was the file that had (Restored) in the name and was truncated)
break.txt
cat.zip
fred.jpg (this was another file that had (Restored) in the name and was truncated)
I hope that makes sense.

Related

Getting a file name into a variable using a batch file

I've got a batch file which needs to perform operations on a specific file each day. So far, the file names have followed the pattern EX_2017-08-30.DAT which means I could use the following to get the exact filename for the day:
set today=%date:~-4,4%-%date:~-10,2%-%date:~-7,2%
set filename=EZ_%today%.DAT
Now I'm being told the filenames will change to include a timestamp, such as EX_2017-08-30-231859.DAT. However, the exact time won't be known beforehand (it gets set when a certain process completes).
I can't use a wildcard throughout the batch file because the filename is being written to an external file for another application to use, so I have to know the exact filename. Is there anyway that I can do a search with a wildcard and store the resulting complete filename into a variable?
If you can list the files in the directory your EX_* file is in, you can do:
for %%i in (EX_%today%-*.DAT) do (
set filename=%%i
)
The first line lists all files in the directory matching the date and the extension, and then it sets the last file to the filename variable. Be careful as this does not throw any warnings should there be more than one file matching the expression.
If you cannot list the directory, your only chance is bruteforce. There are only 24*60*60 possibilities of the filename, and if you go backwards in time, you should reach the desired file in just a couple of thousands of iterations, providing the task is usually completed close to midnight.

How to modify a single file inside a very large zip without re-writing the entire zip?

I have large zip files that contain huge files. There are "metadata" text files within the zip archives that need to be modified. However, it is not possible to extract the entire zip and re-compress it. I need to locate the target text file inside the zip, edit it, and possibly append the change to the zip file. The file name of the text file is always the same, so it can be hard-coded. Is this possible? Is there a better way?
There are two approaches. First, if you're just trying to avoid recompression of the entire zip file, you can use any existing zip utility to update a single file in the archive. This will entail effectively copying the entire archive and creating a new one with the replaced entry, then deleting the old zip file. This will not recompress the data not being replaced, so it should be relatively fast. At least, about the same time required to copy the zip archive.
If you want to avoid copying the entire zip file, then you can effectively delete the entry you want to replace by changing the name within the local and central headers in the zip file (keeping the name the same length) to a name that you won't use otherwise and that indicates that the file should be ignored. E.g. replacing the first character of the name with a tilde. Then you can append a new entry with the updated text file. This requires rewriting the central directory at the end of the zip file, which is pretty small.
(A suggestion in another answer to not refer to the unwanted entry in the central directory will not necessarily work, depending on the utility being used to read the zip file. Some utilities will read the local headers for the zip file entry information, and ignore the central directory. Other utilities will do the opposite. So the local and central entry information should be kept in sync.)
There are "metadata" text files within the zip archives that need to be modified.
However, it is not possible to extract the entire zip and re-compress it.
This is a good lesson why, when dealing with huge datasets, keeping the metadata in the same place with the data is a bad idea.
The .zip file format isn't particularly complicated, and it is definitely possible to replace something inside it. The problem is that the size of the new data might increase, and not fit anymore into the location of the old data. Thus there is no standard routine or tool to accomplish that.
If you are skilled enough, theoretically, you can create your own zip handling functions, to provide the "file replace" routine. If it is about the (smallish) metadata, you do not even need to compress them. The .zip's "central directory" is located in the end of the file, after the compressed data (the format was optimized for appending new files). General concept is: read the "central directory" into the memory, append the new modified file after the compressed data, update the central directory in memory with the new file offset of the modified file, and write the central directory back after the modified file. (The old file would be still sitting somewhere inside the .zip, but not referenced anymore by the "central directory".) All the operations would be happening at the end of the file, without touching the rest of the archive's content.
But practically speaking, I would recommend to simply keep the data and the metadata separately.

Copy a file from network share system cannot find path specified

I have a seriously weird problem, and I suspect it has something to do with the length of the filename - but I cannot alter it (for now).
I'm trying to copy a file from a network share to the users local %temp% with a logon script. The reason for this, is that each user needs to have an individual copy of the file when opening it so that the next users doesn't get a "file is locked" message.
If I run the command manually in a CMD window, the file gets copied, but if I place it in a login.bat-file on the domains netlogon-folder and tries to run the bat-file, I get "The system cannot find the path specified."
This is the actually command:
COPY /Y "\\SERVER\Felles\Administrasjon\Customer KS-manual\Kvalitetshåndbok\2000 org. kart\2 0 B 01-14 Customer Name Arbeids-Prosess-flyt_NO.odg" %TEMP%\Kvalitetshaandbok.odg
So as I said - if I type that in CMD, the file gets copied. If I place it in \customer.local\NETLOGON\login.bat and run the BAT-file, I get "The system cannot find the path specified."
I also tried earlier to just open the file, and that only worked if I opened \customer.local\NETLOGON\shortcut.lnk and shortcut.lnk pointed to the UNC-path of the file. The problem is that I cannot copy a file using a shortcut.lnk AFAIK.
My main object is to have each user get a unique copy of the file and opened on logon.
Is the problem isolated to the length, or is there something else I miss out on?
Try "%TEMP%\Kvalitetshaandbok.odg" with the double quotes because the username can contain spaces etc, which trickles down to the %temp% path.
There are also non-latin characters in the path and the code page being used may need to be altered to match the character set.

Command Prompt Wildcard File Delete Incorrectly Matching

On a regular basis, I am trying to clean up some data folders for an ERP program prior to doing a backup and performing maintenance on the data tables. I've been using Windows Explorer to search for extraneous backup and temporary files prior to the full backup (the maintenance procedures create backup files during the process that aren't always removed), but I'd like to just run it all through a batch file to simplify and speed up the process. I'm filtering with the following:
*NGT????????????.old
*Wrk*????????????.m4t
Also, the command I'm using:
del /S /Q
Both of these work perfectly though the search function within Explorer. The first one works correctly in a command prompt, but the second doesn't. The series of ?s are created by the ERP software as a time stamp, to indicate a copy of the original was created at that time. And the second * represents a one or two character user ID that indicates the user that created the file (it isn't all that important except that the character length isn't always the same). When I try to filter in the command prompt with that second filter, not only does it grab the files I want it to, but it also grabs the original source files which DO NOT have a time stamp on them. For example, the following file names:
File 1) AR_AgedInvoiceReportWrk.M4T
File 2) AR_AgedInvoiceReportWrkTB081615903027.M4T
File 2 is the only one that should be deleted, but it will delete both File 1 and File 2. I've even tried using two or three ?s instead of the second * just to see if a difference would occur, but it doesn't.
Does the command prompt not recognize the ? the way Explorer does? What am I missing?
DIR and it seems other tools match the short file name and the long filename. Your short filenames have wrk as the leading characters and then you are matching a whole swag of any-character.
A solution is to use something like DIR /b /a-d and pipe it through findstr with a regexp, and that will match only the long filenames.

In SQL Server, how to move/import a multiple .trc files to a trace table

I have a set of rollover .trc files recorded with Sql Profiler.
mytrace.trc
mytrace_1.trc
mytrace_2.trc
mytrace_3.trc
I can import the first one using this command:
use [my-database]
SELECT * INTO trace_folder
FROM::fn_trace_gettable('C:\mytrace.trc', 4)
However, this only appears to load the first file, not all four.
You'll want to use fn_trace_gettable:
From http://msdn.microsoft.com/en-us/library/ms188425.aspx:
USE AdventureWorks;
GO
SELECT * INTO temp_trc
FROM fn_trace_gettable('c:\temp\mytrace.trc', default);
GO
Also, a warning from the documentation:
Be aware that the fn_trace_gettable function will not load rollover
files (when this option is specified by using the number_files
argument) where the original trace file name ends with an underscore
and a numeric value. (This does not apply to the underscore and number
that are automatically appended when a file rolls over.) As a
workaround, you can rename the trace files to remove the underscores
in the original file name. For example, if the original file is named
Trace_Oct_5.trc and the rollover file is named Trace_Oct_5_1.trc, you
can rename the files to TraceOct5.trc and TraceOct5_1.trc.
From SQL 2008 BOL ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/c2590159-6ec5-4510-81ab-e935cc4216cd.htm
Be aware that the fn_trace_gettable function will not load rollover files (when this option is specified by using the number_files argument) where the original trace file name ends with an underscore and a numeric value. (This does not apply to the underscore and number that are automatically appended when a file rolls over.) As a workaround, you can rename the trace files to remove the underscores in the original file name. For example, if the original file is named Trace_Oct_5.trc and the rollover file is named Trace_Oct_5_1.trc, you can rename the files to TraceOct5.trc and TraceOct5_1.trc.
This was the problem I had. My Server Side Trace names are _Purpose.trc. What was I thinking when I embedded "" in teh file name :)

Resources