Sign in to follow this  
Followers 0
NDog

for loop directories - best way to copy subfolders - [solved]

7 posts in this topic

#1 ·  Posted (edited)

I'm attemtpting to figure out the best way to make it so directory '$src' replaces the contents of its folders to the subfolders of directory '$dst' if they exist in the subfolder of $dst.

The reason I need to use autoit is because I need to do some string replacements substituing '-' with 'spaces' if needed on some of the folders

eg 'Synaptics PS-2 Port TouchPad' should match 'Synaptics PS 2 Port TouchPad'

eg 'Intel® N10-ICH7 Family PCI Express Root Port -27D6' should match 'Intel® N10 ICH7 Family PCI Express Root Port - 27D6'

So far I have a outline of what I need done in a batch file, however the batch file cannot do any string substituting and it cannot parse ! characters (batch limitation). Also the total project should be done in autoit, and finally I would like to improve my autoit skills.

Any help in getting the string substution matches would be helpful as I have not done this before, also if you can follow the code to help convert to autoit that would be really helpful.

$dst

C:\drivers_doubledriver\BiometricDevice
C:\drivers_doubledriver\BiometricDevice\AuthenTec Inc. AES2501A

C:\drivers_doubledriver\Bluetooth
C:\drivers_doubledriver\Bluetooth\HP Integrated Module with Bluetooth 2.0 Wireless Technology

C:\drivers_doubledriver\Display
C:\drivers_doubledriver\Display\ATI Mobility Radeon X1600

C:\drivers_doubledriver\hdc
C:\drivers_doubledriver\hdc\Intel(R) ICH7 Family Ultra ATA Storage Controllers - 27DF
C:\drivers_doubledriver\hdc\Intel(R) ICH7M-MDH SATA AHCI Controller

C:\drivers_doubledriver\HIDClass
C:\drivers_doubledriver\HIDClass\HP Hotkey Device

C:\drivers_doubledriver\MEDIA
C:\drivers_doubledriver\MEDIA\SoundMAX Integrated Digital HD Audio

C:\drivers_doubledriver\Mouse
C:\drivers_doubledriver\Mouse\Synaptics PS-2 Port TouchPad

C:\drivers_doubledriver\MTD
C:\drivers_doubledriver\MTD\Texas Instruments PCIxx12 Integrated FlashMedia Controller

C:\drivers_doubledriver\Net
C:\drivers_doubledriver\Net\Broadcom NetXtreme Gigabit Ethernet

C:\drivers_doubledriver\SmartCardReader
C:\drivers_doubledriver\SmartCardReader\Texas Instruments PCI GemCore based SmartCard controller

C:\drivers_doubledriver\System
C:\drivers_doubledriver\System\High Precision Event Timer
C:\drivers_doubledriver\System\HP Mobile Data Protection Sensor
C:\drivers_doubledriver\System\Intel(R) 82801 PCI Bridge - 2448
C:\drivers_doubledriver\System\Intel(R) ICH7M-U LPC Interface Controller - 27B9
C:\drivers_doubledriver\System\Intel(R) N10-ICH7 Family PCI Express Root Port -27D0
C:\drivers_doubledriver\System\Intel(R) N10-ICH7 Family PCI Express Root Port -27D2
C:\drivers_doubledriver\System\Intel(R) N10-ICH7 Family PCI Express Root Port -27D6
C:\drivers_doubledriver\System\Mobile Intel(R) 945GM-GU-PM-GMS-940GML-943GML and Intel(R) 945GT Express PCI Express Root Port - 27A1
C:\drivers_doubledriver\System\Mobile Intel(R) 945GM-GU-PM-GMS-940GML-943GML and Intel(R) 945GT Express Processor to DRAM Controller - 27A0

C:\drivers_doubledriver\USB
C:\drivers_doubledriver\USB\Intel(R) N10-ICH7 Family USB Universal Host Controller - 27C8
C:\drivers_doubledriver\USB\Intel(R) N10-ICH7 Family USB Universal Host Controller - 27C9
C:\drivers_doubledriver\USB\Intel(R) N10-ICH7 Family USB Universal Host Controller - 27CA
C:\drivers_doubledriver\USB\Intel(R) N10-ICH7 Family USB Universal Host Controller - 27CB
C:\drivers_doubledriver\USB\Intel(R) N10-ICH7 Family USB2 Enhanced Host Controller - 27CC

$src

C:\drivers_drivergenius\Agere Systems HDA Modem
C:\drivers_drivergenius\ATI Mobility Radeon X1600
C:\drivers_drivergenius\AuthenTec Inc. AES2501A
C:\drivers_drivergenius\Bluetooth Device (Personal Area Network)
C:\drivers_drivergenius\Bluetooth Device (RFCOMM Protocol TDI)
C:\drivers_drivergenius\Broadcom NetXtreme Gigabit Ethernet
C:\drivers_drivergenius\High Precision Event Timer
C:\drivers_drivergenius\HP Hotkey Device
C:\drivers_drivergenius\HP Integrated Module with Bluetooth 2.0 Wireless Technology
C:\drivers_drivergenius\HP Mobile Data Protection Sensor
C:\drivers_drivergenius\Intel(R) 82801 PCI Bridge - 2448
C:\drivers_drivergenius\Intel(R) ICH7 Family Ultra ATA Storage Controllers - 27DF
C:\drivers_drivergenius\Intel(R) ICH7M MDH SATA AHCI Controller
C:\drivers_drivergenius\Intel(R) ICH7M U LPC Interface Controller - 27B9
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family PCI Express Root Port - 27D0
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family PCI Express Root Port - 27D2
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family PCI Express Root Port - 27D6
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family USB Universal Host Controller - 27C8
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family USB Universal Host Controller - 27C9
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family USB Universal Host Controller - 27CA
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family USB Universal Host Controller - 27CB
C:\drivers_drivergenius\Intel(R) N10 ICH7 Family USB2 Enhanced Host Controller - 27CC
C:\drivers_drivergenius\Intel(R) PRO Wireless 3945ABG Network Connection
C:\drivers_drivergenius\Microsoft Bluetooth Enumerator
C:\drivers_drivergenius\Mobile Intel(R) 945GM GU PM GMS 940GML 943GML and Intel(R) 945GT Express PCI Express Root Port - 27A1
C:\drivers_drivergenius\Mobile Intel(R) 945GM GU PM GMS 940GML 943GML and Intel(R) 945GT Express Processor to DRAM Controller - 27A0
C:\drivers_drivergenius\Smart card filter driver
C:\drivers_drivergenius\SoundMAX Integrated Digital HD Audio
C:\drivers_drivergenius\Synaptics PS 2 Port TouchPad
C:\drivers_drivergenius\Texas Instruments PCI GemCore based SmartCard controller
C:\drivers_drivergenius\Texas Instruments PCI-8x12 7x12 6x12 CardBus Controller
C:\drivers_drivergenius\Texas Instruments PCIxx12 Integrated FlashMedia Controller
C:\drivers_drivergenius\Trusted Platform Module 1.2

sort.cmd

@echo off
setlocal enabledelayedexpansion

:: The script is made to organise Drivers backed up with 2 programs, doubledriver and drivergenuis
:: Double Driver makes a great sub-folder structure whereas Driver Genius puts all drivers in one folder
:: Double Driver does not always backup all drivers correctly wheras Driver Genius IMHO backups all drivers properly

:: Scan Double Drivers backup folder structure and use it
:: Match the folders from Driver Genius and overwrite Double Driver backups


set src=drivers_drivergenius
set dst=drivers_doubledriver


rem scan source folder for immediate folders and create array of folders
for /f "tokens=*" %%a in ('dir/b/ad "%~dp0%src%"') do (
    set /a count+=1
    set _src_!count!=%%a
    )

rem parse through the destination folder sub-folders
rem if matches are found to the array then perform operations

rem scan the destination folder
for /f "tokens=*" %%a in ('dir /b "%~dp0%dst%" 2^>nul') do (

    rem scan the sub folders one level deep
    for /f "tokens=*" %%b in ('dir/b/ad "%~dp0%dst%\%%a" 2^>nul') do (

        rem scan the source array and look for a match
        for /f "usebackq tokens=2-3* delims=_=" %%c in (`set _src`) do (

            rem if subfolder name matches array then perform operations
            if "%%b" == "%%d" (

                rem move and replace destination folder with source folder
                echo %%a\%%b

                rd /s/q "%~dp0%dst%\%%a\%%b"
                move /y "%~dp0%src%\%%d" "%~dp0%dst%\%%a\%%b" >nul

                )
            )
        )
    )


pause
Edited by NDog

Share this post


Link to post
Share on other sites



Hi,

check

StringRegExpReplace

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

Could you provide an example, could you show me a way where I could substitue any position in the string to match '-' with 'space' in any position of the string? I am thinking this is slightly more complex than a one liner.. I could be wrong.

eg

'Synaptics PS-2 Port TouchPad' to match 'Synaptics PS 2 Port TouchPad'

'Synaptics PS-2-Port TouchPad' to match 'Synaptics PS 2 Port TouchPad'

'Synaptics PS-2-Port-TouchPad' to match 'Synaptics PS 2 Port TouchPad'

'Synaptics-PS-2-Port-TouchPad' to match 'Synaptics PS 2 Port TouchPad'

'Synaptics-PS 2-Port-TouchPad' to match 'Synaptics PS 2 Port TouchPad'

'Synaptics PS 2-Port-TouchPad' to match 'Synaptics PS 2 Port TouchPad'

etc...

Regards

Share this post


Link to post
Share on other sites

Hi again,

$sInput = "Synaptics PS-2 Port TouchPad"
MsgBox(0, "before", $sInput)
$sOutput = StringRegExpReplace($sInput, "[-]"," ")
MsgBox(0, "After", $sOutput)

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

Thanks November, appreciate your help so far :huh2:

I thought string matching will be trickier than that (coming from dos programming), but seems quite simple to do this.

The next part now is getting the folders to scan and match etc

This is what I have tried so far, any tips or pointers would be nice, I think this code is not too far off. Still new to filelisttoarray

#include <Array.au3>
#Include <File.au3>

$src = "C:\drivers_drivergenius"
$dst = "C:\drivers_doubledriver"

;scan source folder for immediate folders and create array of folders
$srcdirlist=_FileListToArray($src, "*", 2)
_ArrayDisplay($srcdirlist,"$srcdirList")


;scan the destination folder
$dstdir=_FileListToArray($dst, "*", 2)
_ArrayDisplay($dstdir,"$dstdir")


For $a = 1 to $dstdir[0]
    MsgBox(0,"hi",$dstdir[$a])
    ;parse through the destination folder sub-folders
    $xFolder = _FileListToArray($dstdir"\"&"\"$dstdir[$a], "*", 2) ; this code doesn't seem to work, what is wrong? Is the supplied path incorrect?
    _ArrayDisplay($xFolder,"$xFolder")
Next

Share this post


Link to post
Share on other sites

Hi again,

$xFolder = _FileListToArray($dstdir & "\" & "\" & $dstdir[$a], "*", 2)

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks November

Finally I have a working code

I have learnt how to use arrays and hopefully am on the way from breaking ties with for /f loops in cmd and hopefully go to autoit. If anyone wants to improve on my code, it is basicall a straight conversion from cmd to autoit, maybe there are even faster ways.

In the final code I will also add FileInstall and compile including doubledriver and drivergenius and have a switch to run them too etc..

testtest.au3

#include <Array.au3>
#include <File.au3>

$src = "C:\drivers_drivergenius"
$dst = "C:\drivers_doubledriver"

;scan source folder for immediate folders and create array of folders
$srcdirlist=_FileListToArray($src, "*", 2)
;_ArrayDisplay($srcdirlist,"$srcdirList") ;debugging

;parse through the destination folder sub-folders
;if matches are found to the array then perform operations

;scan the destination folder
$aFolders = _FileListToArray($dst)
For $i = 1 To UBound($aFolders) - 1
    
    ;scan the sub folders one level deep
    $xFolders = _FileListToArray($dst&"\"& $aFolders[$i],"*",2)
    For $j = 1 To UBound($xFolders) - 1
        ;MsgBox(0,"",$dst &@LF& $aFolders[$i] &@LF& $xFolders[$j] &@LF&"aFolder..." &$i& "xFolder..." &$j) ;debugging

        ;scan through the $srcdirlist array and if it finds a match with the current loop then do work
        For $k = 1 To UBound($srcdirlist) - 1
            ;MsgBox(0,"",$srcdirlist[$k]&@LF&$xFolders[$j]) ;debug
            
            ; match directly (not needed)
            ;If $srcdirlist[$k] = $xFolders[$j] Then 
            ;   MsgBox (0,"match",$srcdirlist[$k])
            ;   ExitLoop
            ;EndIf
            
            ;replace all '-' with 'space' on strings so they are the same
            $sFsr = StringRegExpReplace($srcdirlist[$k], "[-]"," ")
            $xFsr = StringRegExpReplace($xFolders[$j], "[-]"," ")

            ;move and replace destination folder with source folder
            If $sFsr = $xFsr Then 
                ;MsgBox (0,"match",$srcdirlist[$k]) ; eg Intel(R) ICH7M MDH SATA AHCI Controller ; eg Intel(R) ICH7M-U LPC Interface Controller - 27B9
                
                DirRemove($dst &"\"& $aFolders[$i] &"\"& $xFolders[$j],1)
                DirMove($src &"\"& $srcdirlist[$k],$dst &"\"& $aFolders[$i] &"\"& $xFolders[$j],1)
                
                ExitLoop
            EndIf
        
        Next
    Next
Next
Edited by NDog

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0