Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: List files in batch for loop  (Read 7904 times)

0 Members and 1 Guest are viewing this topic.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
List files in batch for loop
« on: June 20, 2009, 09:40:01 AM »
I want to list the files in a directory and its sub directories. I know this part sounds easy, but I have to cut off the path to the directory and keep the paths within that directory.

So if the path was "H:\Docs\recent\word\google.doc" then it would be displayed as "\recent\word\google.doc". And I can't just cut off 7 characters (the length of "H:\Docs") each time because I may need to change the path later.

Here is what I have. I have to change this so that it doesn't display "H:\Docs" each time.
Code: [Select]
@echo off
for /R H:\Docs %%G in (*.*) do (
echo %%G
)
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Helpmeh



    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: List files in batch for loop
« Reply #1 on: June 20, 2009, 10:49:20 AM »
@echo off
for /r H:\Docs %%G in (*.*) do (
set list=%%G
set list=%list:H:\Docs=%
echo %list%
)
pause > nul
That should work...
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: List files in batch for loop
« Reply #2 on: June 20, 2009, 11:07:05 AM »
No, it didn't work. There was no output.
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Helpmeh



    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: List files in batch for loop
« Reply #3 on: June 20, 2009, 12:45:54 PM »
set list=%list:H:\Docs=%
I think I kow what the problem is...
replace that line with this line.
set list=%list:\Docs=%
after this line, use set again to cut off the first 2 characters (I can't remember and I can't get on a computer right now). That should do it...
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: List files in batch for loop
« Reply #4 on: June 20, 2009, 07:27:15 PM »
helpmeh- have you ever considered actually testing your code before putting it out there?
I was trying to dereference Null Pointers before it was cool.

Helpmeh



    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: List files in batch for loop
« Reply #5 on: June 20, 2009, 07:36:10 PM »
Only if I'm on or near a computer. Which I'm not. Anyway, as my mom would say, that was a "brain fart".
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: List files in batch for loop
« Reply #6 on: June 20, 2009, 08:24:26 PM »
It still doesn't work. I tried changing "set list=%list:H:\Docs=%" to "set list=%list:\Docs=%" and all I got was a loop displaying. . .

ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
etc.

Your code looks right. In fact, I tried something similar to yours before I made this post and it didn't work. I don't know why the set command is not working?!
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

gh0std0g74



    Apprentice

    Thanked: 37
    Re: List files in batch for loop
    « Reply #7 on: June 20, 2009, 09:38:19 PM »
    @OP, what actually are you wanting to do ? ( i am not talking about the for loop. I am talking about the problem you are trying to solve? are you moving files? reading text files? why do you need to have the path cut like that?

    Linux711

      Topic Starter


      Mentor

      Thanked: 59
      • Yes
      • Programming Blog
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: List files in batch for loop
    « Reply #8 on: June 21, 2009, 01:19:03 AM »
    @OP, what actually are you wanting to do ? ( i am not talking about the for loop. I am talking about the problem you are trying to solve? are you moving files? reading text files? why do you need to have the path cut like that?

    I have a directory tree that I am trying to backup with xcopy. I got the backing up part to work. It only backs up files if they have changed or are new. The problem is that if a file has been deleted from the source directory, it still remains in the backup folder. After a while, the backup folder will become full of files that I don't need.

    So why am I asking the question? I am trying to take the path of the backup directory off, so that I can change it to the source directory. I will then check if the file exists in the source and if not, delete the file in the backup.

    There is probably another way to do this, but this is the only way I could think of.
    YouTube

    "Genius is persistence, not brain power." - Me

    "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: List files in batch for loop
    « Reply #9 on: June 21, 2009, 02:01:49 PM »
    I have an idea! Change the backup so that it is like this:
    C:\BACKUP\Docs\file.txt is
    H:\Docs\file.txt and
    C:\BACKUP\FOLDER\file2.txt is
    H:\FOLDER\file2.txt
    or something similar.
    So all you have to do is remove the first 2 characters...then use that variable in the xcopy...xcopy H:%var% C:\BACKUP%var% because %var% begins with a \ it should work...
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    ChaosMageX



      Starter

      Re: List files in batch for loop
      « Reply #10 on: June 21, 2009, 11:37:26 PM »
      It still doesn't work. I tried changing "set list=%list:H:\Docs=%" to "set list=%list:\Docs=%" and all I got was a loop displaying. . .

      ECHO is off.
      ECHO is off.
      ECHO is off.
      ECHO is off.
      etc.

      Your code looks right. In fact, I tried something similar to yours before I made this post and it didn't work. I don't know why the set command is not working?!

      You know that your root directory is a certain number of characters in length, right?
      For instance, H:\Docs is 7 characters in length.
      So, try this command:
      Code: [Select]
      set list=%list:~7,20%
      According to the web page http://www.computerhope.com/sethlp.htm , that should work.
      Unfortunately, the %PATH:str1=str2% syntax doesn't seem to work at all, since it only seems to return nothing.  Although I might be using it wrong.
      « Last Edit: June 22, 2009, 12:43:49 AM by ChaosMageX »

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: List files in batch for loop
      « Reply #11 on: June 22, 2009, 06:20:51 PM »
      @ChaosMageX
      Cutting off like that won't work.
      And I can't just cut off 7 characters (the length of "H:\Docs") each time because I may need to change the path later.
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: List files in batch for loop
        « Reply #12 on: June 22, 2009, 07:29:09 PM »
        @OP if you have gawk for windows(see my sig)
        Code: [Select]
        BEGIN{
        d = ARGV[1]
          gsub(/\\/,"\\\\&",d)
          cmd = "dir /b/s "ARGV[1]
          while( cmd | getline dir) {
        gsub(d,"",dir)
        print dir
          }
          close(cmd)
        }


        save as myscript.awk and on command line
        Code: [Select]
        C:\test>gawk -f myscript.awk H:\docs
        « Last Edit: June 22, 2009, 08:33:44 PM by gh0std0g74 »

        Linux711

          Topic Starter


          Mentor

          Thanked: 59
          • Yes
          • Programming Blog
        • Certifications: List
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: List files in batch for loop
        « Reply #13 on: June 30, 2009, 06:23:15 PM »
        I know its been awhile but I still didn't figure it out. I could get it to work if the set command would start working. Could anyone tell me what is wrong with this "set list=%%g"? Because it is not actually storing anything into the list variable.

        Code: [Select]
        @echo off
        set list=
        for /r H:\Docs %%g in (*.*) do (
         set list=%%g
         echo %list%
        )
        YouTube

        "Genius is persistence, not brain power." - Me

        "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

        Linux711

          Topic Starter


          Mentor

          Thanked: 59
          • Yes
          • Programming Blog
        • Certifications: List
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: List files in batch for loop
        « Reply #14 on: July 03, 2009, 07:44:31 PM »
        YYYYYAHHHHHH!!!  ;D

        I got it to work. There were two things I needed to change in order to get this to work. I added a line "setLocal EnableDelayedExpansion" and I used "!" around the variables.

        Here is what I changed:

        Code: [Select]
        @echo off
        setLocal EnableDelayedExpansion
        set list=
        for /r H:\Docs %%g in (*.*) do (
         set list=%%g
         echo !list!
        )

        After I figured out how to fix this, I was able to remove the H:\Docs and replace it with the source path like this "%sourcePath%!list:%backPath%=!" (include quotes)
        I created a batch file that does everything. It copies only files that have been changed (not every file). It then checks the %sourcePath% for any files that have been removed and deletes them from the backup. After the files are gone it removes empty directories.

        If you like it, I could post it in the batch files section (if there is one).

        Here is the full batch file:

        Code: [Select]
        @echo off
        setLocal EnableDelayedExpansion
        set backCMD=xcopy /c /f /d /e /h /i /r /y
        set backPath=H:\Docs
        set sourcePath=I:

        echo Backing up %sourcePath%...

        rem BACKUP THE FILES THAT HAVE BEEN CHANGED
        %backCMD% %sourcePath% %backPath%

        echo Removing old stuff in %backPath%...

        rem CHECK FOR FILES THAT HAVE BEEN REMOVED FROM
        rem THE SOURCE AND DELETE THEM FROM THE BACKUP DIRECTORY
        for /r %backPath% %%g in (*.*) do (
        set var=%%g
        if not exist "%sourcePath%!var:%backPath%=!" (
        echo File:!var!
        del /f /q "!var!"
        )
        )

        rem REMOVE ANY EMPTY DIRECTORIES FROM BACKUP
        for /d /r %backPath% %%g in (*) do (
        set var=%%g
        if not exist "%sourcePath%!var:%backPath%=!" (
        echo Dir:!var!
        rd /s /q "!var!"
        )
        )

        echo %date% : %time%>> backup.log
        echo Finished backing up %sourcePath%
        echo Log entry added in backup.log
        pause
        exit
        YouTube

        "Genius is persistence, not brain power." - Me

        "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe