0

Something went wrong with backup and recovery when I was resetting my laptop. Now I am left with all my files looking like this:File names after error in backup

I tried to re-add the drive as a recovery option through windows recovery (hoping that if I was able to do the recovery, the software would automatically bring all these files back onto my laptop without the backup date information), but the windows recovery tool refused to recognize the FileHistory that had been saved onto this drive.

Now I am looking to just cut-paste (move) these files onto the laptop and am looking for a way to remove that backup date information.

I have already done so manually for a few files before realizing there must be a way to automate this tedious task.

Is there a way to rename all these files in a way so that the backup date information is removed without having to download any external software?

I read through How can I mass rename files? but am still unsure of what to do.

Preferences:

  • Rename happens while the entire folders are moved from external drive to the laptop
  • Not have any changes made to file details such as "Date Modified"

EDIT:

Example: The backed-up file is at the following location on my external drive:
D:\FileHistory\myuser\LAPTOP-78RBSL7E\Data\C\Users\myuser\Desktop\Education\High School\G12\Politics

It should copy to this location on my laptop:
C:\Users\myuser\Desktop\Education\High School\G12\Politics

The filename should go from Culminating Essay (2021_05_02 18_14_18 UTC) to Culminating Essay

This name change should also apply to zip files

AR.C
  • 47
  • So basically remove the part between parentesis? – Ricardo Bohner Oct 14 '21 at 02:27
  • Where should the files be moved to? – Ricardo Bohner Oct 14 '21 at 02:35
  • @RicardoBohner please see edit regarding file name change. As for file movement, it should go back to the location where it was at time of backup, however I believe it would be better as a separate question regarding messed-up recovery, and thus have only indicated it as a preference – AR.C Oct 14 '21 at 02:55
  • Probably https://stackoverflow.com/questions/39861138/powershell-remove-inside-square-brackets – Martheen Oct 14 '21 at 03:59
  • in the long run, excluding additional software is detrimental to your end goals. the requirement just leaves you with scripting. are you prepared to write a script? – Frank Thomas Oct 14 '21 at 04:58
  • @MartheenCahyaPaulo I tried using that on a test document using get-childitem .* | foreach { move-item ..\TestRenameDest $_.name ($_.name -replace ' \(.*\)', '')} but it did not work. I was in ..\TestRenameSrc while I did that. please advise if there is something I am doing wrong. – AR.C Oct 14 '21 at 13:47
  • @FrankThomas you are correct, however I would like to do this without additional software if possible. Yes I am willing to write a script. Also, I am also prepared to accept using external software (as a last resort) as per the answer in the other linked question – AR.C Oct 14 '21 at 13:49
  • Based on the answers, it seems like you've come to the right place! best of luck! – Frank Thomas Oct 15 '21 at 04:09

2 Answers2

2

You can create a batch file to accomplish the task. You copy the code below to notepad and save it with the name that you want but with a *.bat extension.

Change the this part according to your needs and click the batch file set Source= set Destiny=

@echo off
:: here you infrorm the script where the source and destiny are:
set Source=D:\FileHistory\myuser\LAPTOP-78RBSL7E\Data\C\Users\myuser\Desktop\Education\High School\G12\Politics
set Destiny=C:\Users\myuser\Desktop\Education\High School\G12\Politics

:: This gets the full path of the source: for /f "delims=" %%a in ("%Source%") do set Source=%%~dpnxa :: This creates the destiny in case it doesn't exist already: IF not exist "%Destiny%" md "%Destiny%"

:: This sends the full path of each source file to the rename funcion For /f "Delims=" %%a in ('dir /b /s /a-d "%Source%*"') do call :Rename "%%~a"

exit

:: Here the renaming is done: :Rename set "PartName=%~1" call set "PartName=%%PartName:%Source%=%%" for /f "tokens=1,3 delims=()" %%a in ("%PartName%") do set "PartName=%%~a" if "%PartName:~-1%"==" " set "PartName=%PartName:~0,-1%" IF /i not exist "%Destiny%%PartName%%~x1" echo F |xcopy /q /k "%~1" "%Destiny%%PartName%%~x1" goto :EOF

enter image description here

  • Could you please explain which part does what so that I can understand how the script works? Especially in regards to which part removes the date information. – AR.C Oct 14 '21 at 13:38
  • I added some comments in the answer... – Ricardo Bohner Oct 14 '21 at 14:00
  • While testing the code on a test file, as I tested it multiple times, I noticed that it automatically overwrites if same file exists in destination. Is there a way to have it pause and ask if I want to overwrite? If not, I will just be more careful and delete copies before running the script. Also, to just rename only in the same folder, I would simply set destination = source correct? – AR.C Oct 14 '21 at 14:17
  • Fixed, now it only copies files that don't exist in the destiny. – Ricardo Bohner Oct 14 '21 at 14:28
1

Not the most efficient PowerShell code, but the most straight-forward off the top of my head.

$BackupFileRoot = 'D:\FileHistory\myuser\LAPTOP-78RBSL7E\Data\C\Users\myuser'
$RestoreToRoot  = 'C:\Users\myuser'

Recreate Directory Structure

(Get-ChildItem $BackupFileRoot -Directory -Recurse).FullName | ForEach{ $RestorePath = $_.Replace( $BackupFileRoot , $RestoreToRoot ) If ( !( Test-Path $RestorePath ) ) { md $RestorePath -Force | out-null } }

Restore Files

$RegExFind = ' ([\w ]+UTC)' Get-ChildItem $BackupFileRoot -File -Recurse -Force | ForEach{ $RestorePath = $.FullName.Replace( $BackupFileRoot , $RestoreToRoot ) -replace ( $RegExFind , '' ) If ( !( Test-Path -LiteralPath $RestorePath ) ) { Copy-Item -LiteralPath $.FullName $RestorePath } }

AR.C
  • 47
Keith Miller
  • 9,620
  • While testing, (copy pasted the provided code and changed the file paths to reflect test folder) the script works well in terms of creating the folders, but as soon as it gets to the files, it produces an error. When checking the newly created folder, I can see that it created a new folder inside the target directory with the name of one of the files, which seems to be the point at which it throws the error. I am attaching the screenshot of the error it produces. https://imgur.com/a/ViywMO7 – AR.C Oct 15 '21 at 02:29
  • You may have grabbed the code before I made an edit. Your screensshot is showng the code for the folder creation, In the file copy section, the assignment is $RestorePath = $_.FullName.Replace( $BackupRoot , $RestoreRoot ) -replace ( $RegExFind , '' ). Becasue the pipeline object is a FileInfo object, we have to access the FullName property. – Keith Miller Oct 15 '21 at 03:59
  • Looking at the screenshot again, it looks like the error was in the folder creation section. Maybe it just ran into a null or empty string at the end -- sometimes recuresion hiccups, it seems. If the folders have been created, you could now try just the path assignments and file copy code. If you have to post more error messages, please do so by copying the text and adding it as an update to your question. – Keith Miller Oct 15 '21 at 04:39
  • It (newly copied code) is still not working. On looking through the code again, I see that you initially declare the variables(?) BackupFileRoot and RestoreToRoot, but when assigning to RestorePath you use BackupRoot (without FILE) and RestoreRoot (without TO). Could this be the reason behind the code not working properly? However, I am no longer getting the error, but as you see in this screenshot, the script runs but no perceptible action has been committed https://imgur.com/a/bIc7pTW – AR.C Oct 15 '21 at 04:44
  • Apologies, page did not refresh until I had already posted the other comment. As for running it with file copy only, that would mean commenting out the code between your two comments (recreate dir, restore file) correct? – AR.C Oct 15 '21 at 05:04
  • I changed the code on my computer as per my suggested edit to your answer. The error was as I suspected in that the variables/pointers used in the Directory creation and file restoration did not correspond to the declarations. – AR.C Oct 15 '21 at 05:20
  • I was having too much trouble following these comments. Didn't realized you'd edited my post. Didn't realized I was now testing code that had been edited and couldn't understand... – Keith Miller Oct 15 '21 at 21:04