Windows Short name does not match long name - windows

I am copying a list of files using a prefix (i.e., ABCD*) to match files in a batch script. However, some files that appear to match are being left behind while other files that don't match are getting grabbed.
I ran a dir /X and found that the shortname for a handful of the files didn't match their longname:
4/17/2015 02:04 PM 554 ABCDEF~1.TXT abcdefghijklmnopqrs.txt
4/17/2015 02:08 PM 123 ABCDEF~2.TXT 1234567890.txt
4/17/2015 03:18 PM 233 987654~1.TXT abcdefg123456.txt
Any idea why something like this might happen and how to resolve it?

If your sample data is representative of your actual files, you could specify ABCDEFG* to workaround this issue.
EDIT
Since the above suggestion is not an option, you could use FSUTIL to remove all of the 8.3 names.
This command will analyze the files in the current directory (.) and display the changes without actually making them.
fsutil 8dot3name strip /t .
Remove the /t parameter to actually remove the 8.3 names.
You can also run:
fsutil 8dot3name strip
to see all of the options.

Short and long file names are not required to match. The default algoritm is documented here under "How NTFS Generates Short File Names". You can also find it in the wikipedia
You can change the short file name with
fsutil file setshortname longFileName shortFileName

Related

Updating "Date Created" after copying files

I have recently used iCloud for Windows (10) to download all pictures from iCloud onto my local machine. I then wanted to create a backup so I copy-pasted all (15'000) pictures onto an external hard disk.
I noticed that because of the copy-paste action, the "Date Created" has -- in hindsight obviously -- been changed from the date that the picture was taken to the date / time of the copy action.
Since the copy-action and before I noticed the change of date, I have put many hours into putting pictures in subfolders, etc. I would now like to put the original date/time back into the metadata.
My idea is to make a dirlisting of the iCloud original archive, returning filename, md5 hash, and Date Created. I then want to write a script (Powershell?) to find the matching file in my subfolders, and update the date.
A few questions:
Does this seem like the best approach?
Is there a better way to copy photo's in the future, keeping the original Date Created?
Any help into the right direction whether this is a good idea with Powershell, would be greatly appreciated.
Manually changing timestamps using PowerShell
To change the Date Created, Date Modified, and Date Accessed file properties using PowerShell, I'd recommend checking out this website here. It explains how to change the Date Created/Modified/Accessed file properties for a single file, for all files in a given folder, or even for a folder itself. For example:
For a single file:
To set the date created time for "filename.txt" to 5 December 2012 at, say, 9:57:05 PM, you'd write:
(Get-Item "C:\Users\Path_to_file\filename.txt").creationtime=$(Get-Date "05/12/2012 21:57:05")
Similarly, to set the date modified, you'd write:
(Get-Item "C:\Users\Path_to_file\filename.txt").lastwritetime=$(Get-Date "05/12/2012 21:57:05")
and for date accessed time:
(Get-Item "C:\Users\Path_to_file\filename.txt").lastaccesstime=$(Get-Date "05/12/2012 21:57:05")
You can event set properties to other properties. For example:
To set the date created equal to the date modified, you'd write:
(Get-Item "C:\Users\Path_to_file\filename.txt").creationtime=$(Get-Item "C:\Users\Path_to_file\filename.txt").lastwritetime
For all files in a folder named "Test":
Get-ChildItem -force 'C:\Users\Path_to_folder\Test\' * | ForEach-Object{$_.CreationTime = ("3 August 2019 17:00:00")}
Get-ChildItem -force 'C:\Users\Path_to_folder\Test\' * | ForEach-Object{$_.LastWriteTime = ("3 August 2019 17:10:00")}
Get-ChildItem -force 'C:\Users\Path_to_folder\Test\' * | ForEach-Object{$_.LastAccessTime = ("3 August 2019 17:10:00")}
Note that the -force parameter ensures that hidden files are also included. Also, keep in mind that if you want to change each picture's timestamps to that of its own corresponding image (i.e, if you want to intelligently automate the process), you'll need to write a script that takes care of each separate case.
Copy files while preserving timestamps (and more)
The simplest tool to use is Windows' own, built-in tool: robocopy. See Microsoft's documentation on the robocopy command here. You just run the program via the command prompt (with administrator privileges).
For your needs, suppose you have images and videos in their original form (the ones that have all the correct timestamps, etc.) in the folder "Original", located at "C:\Users\Person\Desktop\Original", and you'd like to copy all those images and videos to a folder located on your external hard drive, at "D:\Pictures\Copied". The following command would probably work best:
robocopy "C:\Users\Person\Desktop\Original" "D:\Pictures\Copied" *.* /e /copy:DAT /dcopy:DAT /mt:16 /j /xjd /xa:s /r:1 /w:0 /log:"filename_path.txt"
Each argument is explained in detail on Microsoft's documentation page for robocopy, but I'll explain them here too.
The 1st argument specifies the path to the source folder. Please note that there are no trailing backslashes! If you include a backslash at the end, robocopy won't understand the input.
The 2nd argument specifies the path to the destination folder.
The 3rd argument can actually be multiple arguments. Here, the *.* specifies the file or files to be copied. Since wildcard characters (* or ?) are supported, *.* matches all files in the source directory (i.e., everything will be copied). *.* is the default parameter, so if you want all files to be copied, you don't even need to specify this parameter. If you want to copy, say, all files that end in .jpg, you'd write *.jpg. Or, if you want to copy, say, just two files file1.jpg and file2.mp4, you'd write those out explicitly, one after each other, as such robocopy "C:\Users\Person\Desktop\Original" "D:\Pictures\Copied" file1.jpg file2.mp4 /e /copy:DAT /dcopy:DAT /mt:16 /j /xjd /xa:s /r:1 /w:0 /log:"filename_path.txt".
The 4th argument /e copies subdirectories. This option automatically includes empty directories.
/copy:DAT specifies which file properties to copy. Here, D, A, and T means that the file's Data, Attributes, and Time Stamps properties will be copied. Refer to robocopy's documentation for more details.
/dcopy:DAT same as /copy:DAT except for folders.
/mt:16 creates multi-threaded copies with 16 threads. You can specify an integer between 1 and 128.
/j copies using unbuffered I/O (recommended for large files).
/xjd excludes junction points for directories.
/xa:s excludes "System" files.
/r:1 specifies the number of retries on failed copies. Here, it's set to 1. The default value is 1,000,000 (one million retries)!
/w:0 specifies the wait time between retries, in seconds. Here, it's set to 0, so 0 seconds are spent waiting.
/log:"filename_path.txt" writes the status output to the log file.

Copy an image and rename it to match other file names

I have ZERO bat knowledge so thought I would ask here, if I may.
I have an image C:\Users\Dane\Pictures\Doom.jpg. I wish for this image to be copied and for it to be renamed to the exact name of 379 non-image type files which are in another folder which is G:\Doom. So I will have the same image 379 times but named to match the 379 files.
Would anyone be kind enough to write a bat file to do that? Thank you in advance.
This site is not a free code writing service; Rather give the subject an attempt and we will be happy to assist you. However, because I'm nice, I have a response for you.
This problem is pretty common and can be solved very easily using a FOR statement. In this example we will be searching a directory for every item stored inside. Each item will be added to the integer %%A. For more information do FOR /? inside a command window.
for %%a in ("Directory") DO (Action)
For copying files, we will use the copy command. Please keep note that we will be using parameter extensions to expand the %%A to have no extension using %%~na. More info here: Parameter Extensions
This script will copy & rename Doom.jpg to G:\Doom for each item in the directory.
Batch File:
for %%a in ("G:\Doom\*") do (copy "C:\Users\Dane\Pictures\Doom.jpg" "G:\Doom\%%~na.png")
Command Prompt:
for %a in ("G:\Doom\*") do (copy "C:\Users\Dane\Pictures\Doom.jpg" "G:\Doom\%~na.png")

Why file cannot be found to copy

I am writing a batch script which will copy a file from a folder into the C:\ drive:
#ECHO ON
COPY C:\RANDOMFILES\Weekly Reprort_Hew*.xls C:\Weekly Reprort_Hew???????????.xls
The filename in the RANDOMFILES folder is: Weekly Reprort_Hew, 6-29-2014 10-30-00 PM-642.xls (The date and time and the number at the end will always change so I used the * in the filename being copied in the script)
When I run the batch script, I get the following message:
c:\RANDOMFILES>COPY C:\RANDOMFILES\Weekly Reprort_Hewlett*.xls C:\Weekly Reprort
_Hewlett???????????.xls
The system cannot find the file specified.
How can I fix the issue?
You need double quotes to handle spaces etc. Double check the spelling too.
#ECHO ON
COPY "C:\RANDOMFILES\Weekly Reprort_Hew*.xls" "C:\Weekly Reprort_Hew???????????.xls"
Don't know why it is not working - is it hidden? Maybe the spaces in the name?
The following will work though:
FOR %%I in (C:\RANDOMFILES\Weekly Reprort_Hew*.xls) DO COPY "%%I" C:\
The destination file name isn't necessary; if not otherwise specified, it will remain unchanged provided the destination is different. That may be why the plain COPY command isn't working.
Also:
"It is an not-so-well-known fact that the question mark wildcard will match exactly one character only when the wildcard does not appear at the end of a file name. " from http://www.thefriendlycoder.com/2011/11/24/batch-file-gotcha-question-mark-wildcard/

find (ms-dos command) looking into sub directories files

I am trying to use find to look for files that contain a specific keyword. To my understanding, find takes in a file not a directory (that's why it is giving me error). So is there a way i can go through each sub-directories and look into each and every file to execute the find command so that I can get the result of all files that contains the given keyword? (Much like grep)
So far i got this:
find \S "keyword" "directory\ *"
Error i am getting:
Access denied - Directory name
Access denied - Directory name
.
.
.
Anyone give me a hint? I am current using window 7 right now.
FIND does not take the /s switch (not \s - that's a directory)
FINDSTR is another animal.
About the only quibble about the documentation avaliable from findstr /? from the prompt is that you can target a filemask in a specified directory by specifying \dirname\*, not simply in the current directory as documented. There is also an option to run against a semicolon-delimiter list of directory names - but I've never seen it used.

Windows batch file: rename files (possibly in multiple folders) based on input file (of target filenames)

I am a Batch-newbie, so please accept my apologies and Thanks in advance !
This "tool" is to automate the slimming down of Windows (XP) by disabling certain system driver, DLL and EXE files. Instead of outright deletion, I wish to rename-in-place, thus "removing" them from the OS, but not losing sight of where they belong (should any need to be "restored"). Renaming is accomplished by appending a new suffix to the existing filename (eg: "wdmaud.drv.group_1") The renaming suffix should be another input variable.
The target-list is approx. 1100 files long (divided into various groups/phases), so manual renaming is out of the question. Each group will be processed in a separate run of the batch file, varying the target-list input file for each execution.
Target-list is plain text file, one filename per line (no other data in the files). Number of entries per group varies. Target list will look like this:
-- example start --
netapi.dll
netcfgx.dll
netdde.exe
netevent.dll
neth.dll
netid.dll
netrap.dll
nic1394.sys
-- example end --
Filenames may be in UPPER, lower, or MiXeD case. The files may be present in more than one folder in the C:\Windows hierarchy - or may not be present at all. If a file is not found anywhere in the system, it's name should be written to a text file, one-entry-per-line.
The specific folders of interest are:
C:\WINDOWS\
C:\WINDOWS\system\
C:\WINDOWS\system32\
C:\WINDOWS\system32\dllcache
C:\WINDOWS\system32\drivers
The renaming will be done by connecting the target OS drive to another XP computer, so locked system files should not be a problem.
Any help you can offer will be greatly appreciated.
a double FOR loop may help you.. this is a very simple example, just to get you started
for /f "tokens=*" %%f in (%targetlist%) do (
for /f "tokens=*" %%d in (%dirlist%) do (
if exist "%%d\%%f" echo %%f found in %%d
)
)
see HELP FOR.

Resources