简体   繁体   中英

How to copy files to different places based on .txt file content with batch script

I am trying to create set of scripts to take backups and then later restore those backups. These scripts are suppose to be able to use on different sets of files to backup/restore.

I have file data.txt that has this kind of content:

C:\data\set_one\data111.ini
C:\data\set_one\data222.ini
C:\data\set_one\executable.exe
C:\data\set_one\extra\*.obj

This file is used to create backups for which I've successfully created script. All those files are backed up to one single folder. Now my problem is how to restore all these back to original places using address from that one data.txt file to correct locations. I was thinking something like this:

@echo off
title Configurations restoration...

Rem Restore configurations from backup
set /p Device=<SET_DEVICE.txt

if exist %Device%_Config_Backup (
    for /F %%F in ('dir /b %Device%_Config_Backup') do (

        set /p Replace=<find "%%F" Data\%Device%_CONFIG.txt
        - how to get file location?
        Call Copy "%CD%\%%F" Replace/y
        
    )
    
) else (
    echo Missing backup configurations
)

My Replace part does not work nor do I know how to get file location from line without file name part. As added problem there are also *.obj cases, how to get those in correct place?

-- UPDATE --

SET_DEVICE.txt has list of available device names and script only picks first row in file and uses that. If user needs to change device, they change first line text to other one from list. This is so user does not ever need to open console themself (devices are not changed often, easier to change one text file row now and then).

Based on this device, folder %DEVICE%_Config_Backup is created. To this single folder all wanted files are copied. This folder is created to location from where script is run from, such as from memory stick.

Text file datax.txt is the file that tells what files and from where they are copied as backup because only specific files are wanted as backup. There are several data.txt files in data folder, one for each device (example datax, datay, etc.).

Finally when backups are meant to be restored, all files in folder %DEVICE%_Config_Backup need to be put back to their original places that can be seen in data.txt file (see example above, the first grey code block).

文件夹结构

Picture of the folder and structure I was thinking, hopefully it helps clarifying

--UPDATE 2.1--

I made slight change to those data files format so now they are (this would be DEVICEX_CONFIG.txt located in Data folder):

C:\data\set_one\    data111.ini
C:\data\set_one\    data222.ini
C:\data\set_one\    executable.exe
C:\data\set_one\extra\  *.obj

This way I can still fill original purpose and I can now more easily separate filename from file location as needed.

This unfortunately still does not solve my biggest issue in how to restore backup files from single folder (example the DEVICEX_Config_Backup folder) to their original places using single.txt file like one above that holds file location and file name, separated by tab.

My attempt I believe to be closest to working solution is something like this:

@echo off
title Configurations restoration...
SETLOCAL EnableDelayedExpansion
cls

Rem Restore configurations from backup to original locations
set /p Device=<SET_DEVICE.txt

if exist %Device%_Config_Backup (
    FOR /F "tokens=1 delims=    " %%A IN ('dir /b %Device%_Config_Backup') DO (
        set /p destination='FIND "%%A" Data\%Device%_CONFIG.txt'
        echo source: %Device%_Config_Backup\%%A
        echo destination: %destination% 
    )
    
) else (
    echo Missing backup configurations
)

Once I get these echo parts to display correct source and destination, its easy enough to swap command to copy. Unfortunately my closest to working does not work and I don't know how to get it working.

This was prepared following my understanding of your question, if the case is completely different, please let me know...

  1. The SET_DEVICE.txt file saves/keep the device name

  2. The device name stored in SET_DEVICE.txt , composes the file name Config_Backup.txt in a concatenated way

  3. The final task would be to copy the files listed in ._Device!_Config_Backup.txt to the current folder, the same as your file

@echo off && setlocal enabledelayedexpansion

cd /d "%~dp0" && title <nul && title Configurations restoration...
if exist "%~dp0\Data\SET_DEVICE.txt" ( set /p _Device=<"%~dp0\Data\SET_DEVICE.txt"
     goto :_restore_files ) else echo\Error: Missing backup folder\files configurations...
     endlocal && >nul pause && goto :EOF

:_restore_files
2>nul (for /f tokens^=* %%i in ('type "%~dp0\Data\!_Device!_Config_Backup.txt"')do copy /y "%%~i" . >nul
 ) && ( echo\Device: !_Device! & echo\Configuration files restored^^!!... & endlocal & pause & goto :EOF 
 ) || ( echo\Error: Missing your backup folder\files configurations^^!!.. & endlocal & pause & goto :EOF )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM