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

Author Topic: DOS Batch  (Read 16409 times)

0 Members and 1 Guest are viewing this topic.

ionic

    Topic Starter


    Rookie

    DOS Batch
    « on: January 22, 2008, 06:09:33 PM »
    I'd like to do 2 things with this batch file:

    1) either rename a specific to a random name (example: qpwo.xyz)
        if this can not be done then to a specific name. xxxx.xxx
    then:

    2) delete the file.

    In the end I'd like to drag-n-drop a file abc.def onto a shortcut and have it rename
    abc.def to qlsybc.fhw and then delete qlsybc.fhw.

    any help would be appreciated.

    gpl



      Apprentice
    • Thanked: 27
      Re: DOS Batch
      « Reply #1 on: January 22, 2008, 06:45:58 PM »
      Your first part could be solved by

      ren %1 %RANDOM%.%RANDOM%

      now, why on earth do you want to rename a file, then delete it ? why not just delete it in the first place ?

      Graham

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: DOS Batch
      « Reply #2 on: January 22, 2008, 06:59:50 PM »
      My guess would be it's a file he doesn't have full permission to delete...

      But i've been wrong before.
      " Anyone who goes to a psychiatrist should have his head examined. "

      ionic

        Topic Starter


        Rookie

        Re: DOS Batch
        « Reply #3 on: January 22, 2008, 07:51:18 PM »
        Why on earth would I want to rename a file then delete it. Fir example if I had a file called finance.xml and I deleted it to the recycle bin it would still be recoverable, and it could have obvious value to someone trying to hijack personal information. But if I rename it to something meaningless then delete it. those snoops would look elsewhere for important documents.

        Nope, not trying to delete anything I do not or should not have access to.

        ionic

          Topic Starter


          Rookie

          Re: DOS Batch
          « Reply #4 on: January 22, 2008, 07:59:19 PM »
          Using the following code I get:

          Code: [Select]
          ren %1 %RANDOM%.%RANDOM%
          sdelete -p 12 -s %1
          exit

          The fie was renamed to some random file name but this file remained on the system while the original file was deleted,

          gpl



            Apprentice
          • Thanked: 27
            Re: DOS Batch
            « Reply #5 on: January 23, 2008, 03:21:40 AM »
            In that case, you need to store the new filename first

            Code: [Select]
            Set newname=%RANDOM%.%RANDOM%
            ren %1 %newname%
            sdelete -p 12 -s %newname%
            exit

            I like your thinking, however, unless you have a third-party app installed, when you delete using the batch Del command, they do not appear in the recycle bin, they are gone - which has caused me some grief over the years !!

            Graham

            CameronY



              Intermediate

              Re: DOS Batch
              « Reply #6 on: January 23, 2008, 05:39:26 AM »
              Guys, just reading through the tread ...

              What is `sdelete` and from what tool box can it sought from ??

              Cheers,
              Cameron

              ionic

                Topic Starter


                Rookie

                Re: DOS Batch
                « Reply #7 on: January 23, 2008, 11:08:30 AM »
                CameronY,

                SDelete.exe is command line file that is capable of secure fle deletion. The company that offered it, Sysinternals, no longer exist as it was bought out by microsoft. The only link I can find for downloading it is here:

                http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx

                Other usefull info I just found on it can be found here:

                http://forum.sysinternals.com/forum_posts.asp?TID=6065&PN=1


                gpl,

                Thanks for the simple, but crucial code. The code you provided I will be setting up in the right-click contex menu for single file deletion.  In terms of batch mode I'd have to know and list every file that I would want to delete in a batch file, which gets quite complicated.

                --------------------------------------------------------------

                after running the new batch file I received the following error:

                SDelete is set for 12 passes. - (Not a problem)
                No files found that match 29517.1883. - (Clueless here!)

                In other words the random rename works.
                the file 29517.1883 does exist in the same location as the original file
                but sdelete did not delete the file.

                Could it be that the sdelete operation is trying to delete before the file has been renamed?
                Not sure....

                -----------------------------------------------------------------

                Nope, not a timing issue. Maybe sdelete.exe is not recognizing the variable, 'newname'.
                Nothing ever comes easy dose it?
                « Last Edit: January 24, 2008, 12:55:33 PM by ionic »

                ionic

                  Topic Starter


                  Rookie

                  Re: DOS Batch
                  « Reply #8 on: January 25, 2008, 08:39:59 PM »
                  There is something fundamentally wrong with this code.
                  Code: [Select]
                  @echo off
                  ren %1 %RANDOM%.%RANDOM%
                  DEL %1
                  exit

                  The renamed file can not be Deleted using Sdelete.exe, DEL, ERASE
                  The file renaming works fine, but the del, erase...etc. does nothing

                  Any ideas why?

                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: DOS Batch
                  « Reply #9 on: January 26, 2008, 05:02:22 AM »
                  You're trying to delete the file that you just renamed! The earlier post by GPL had it right:

                  Code: [Select]
                  @echo off
                  Set newname=%RANDOM%.%RANDOM%
                  ren %1 %newname%
                  DEL %newname%
                  exit

                  A suggestion: leave echo on while debugging. It will give you some insight into how your file is executing.

                   8)
                  « Last Edit: January 26, 2008, 05:54:34 AM by Sidewinder »
                  The true sign of intelligence is not knowledge but imagination.

                  -- Albert Einstein

                  ionic

                    Topic Starter


                    Rookie

                    Re: DOS Batch
                    « Reply #10 on: January 26, 2008, 10:41:34 AM »
                    OK,
                    Here is the code I used:
                    Code: [Select]
                    Set newname=%RANDOM%.%RANDOM%
                    ren %1 %newname%
                    DEL %newname%
                    pause
                    exit

                    ...and here is reply I received:

                    C:\donume~1\User\>Set newname=15624.19141
                    C:\donume~1\User\>ren "C:\donume~1\User\desktop\test.txt" 15624.19141
                    C:\donume~1\User\>DEL 15624.19141
                    Could Not Find C:\donume~1\User\>15624.19141


                    Conclusion: If the file "test.txt" and the batch file are located on the User desktop, why does DOS
                    lose this positioning and look to delete the file in a directory above the originating directory?
                    The renamed file does indeed replace the test.txt file in the same location, on the desktop.

                    Do I need to set the initial DIR... cd %1 or something to that effect?? Disregaard, this tries sets the dir
                    as test.txt.

                     
                    « Last Edit: January 26, 2008, 10:58:49 AM by ionic »

                    Sidewinder



                      Guru

                      Thanked: 139
                    • Experience: Familiar
                    • OS: Windows 10
                    Re: DOS Batch
                    « Reply #11 on: January 26, 2008, 01:00:01 PM »
                    Quote
                    why does DOS
                    lose this positioning and look to delete the file in a directory above the originating directory

                    You were never in the desktop directory, you only pointed to it. The command window defaults to %homedrive%%homepath% when opened unless overridden. That would be C:\donume~1\User\ in your case. The set statement ran because it contains no path pointers. The ren statement ran because the %1 parameter included a fully qualified file name. You were in one directory but pointed the command parameters to another where it found the file and renamed it. The del failed because the file is not in C:\donume~1\User\ but in C:\donume~1\User\desktop\ and there are is no path pointer to the file in which case the current directory applies: C:\donume~1\User\

                    Code: [Select]
                    cd C:\donume~1\User\desktop
                    Set newname=%RANDOM%.%RANDOM%
                    ren %1 %newname%
                    DEL %newname%
                    pause
                    exit

                    The example above logs on to the correct directory. An added benefit is that you need only pass test.txt on the command line.

                    It's a lot easier if you run batch files from the command line rather than Windows. 8)

                    « Last Edit: January 26, 2008, 01:14:52 PM by Sidewinder »
                    The true sign of intelligence is not knowledge but imagination.

                    -- Albert Einstein

                    ionic

                      Topic Starter


                      Rookie

                      Re: DOS Batch
                      « Reply #12 on: January 26, 2008, 01:29:10 PM »
                      If the SET command knows the full path to the file being deleted and
                      the REN command knows the full path, why does the DEL command fail to
                      recognize the full path to the file.

                      The new code changes nothing really.  One would have to redirect the directory
                      manually every time you ran the bat file from a different location. Thus if I were to
                      drag a file to be deleted onto a shortcut to the bat file, how can it automatically lock in
                      the target path for all action to take place...regardless of where the source file is located?


                      However since I was intending to use sdelete.exe as the final command instead of DEL
                      I just found out that deleting with sdelete renames the shredded file as ZZZZ.ZZZ in place
                      of test.txt.

                      This is the current code using sdelete.exe
                      Code: [Select]
                      @echo off

                      :: The (%*) allows for multiple directories and files to be deleted recursivly.

                      FOR %%F IN (%*) DO ECHO Y| cacls %%F /T /C /G Administrators:F
                      :Set proper attributes for directories
                      FOR %%F IN (%*) DO attrib -h -s -r -a %%F /S /D
                      :Set proper attributes for files
                      FOR %%F IN (%*) DO attrib -h -s -r -a %1\*.* /S /D
                      FOR %%F IN (%*) DO sdelete -p 12 -s -q %%F

                      I still would like to understand how to correct this in a bat file can accomplish this.
                      « Last Edit: January 26, 2008, 02:02:55 PM by ionic »

                      Sidewinder



                        Guru

                        Thanked: 139
                      • Experience: Familiar
                      • OS: Windows 10
                      Re: DOS Batch
                      « Reply #13 on: January 26, 2008, 07:01:54 PM »
                      Quote
                      One would have to redirect the directory manually every time you ran the bat file from a different location

                      Yes indeed, unless the code was generic (see below)

                      Quote
                      If the SET command knows the full path to the file being deleted

                      Set newname=15624.19141
                      It doesn't; it sets a variable to a string; might be a file name; definitely not an included path

                      Quote
                      the REN command knows the full path

                      ren "C:\donume~1\User\desktop\test.txt" 15624.19141
                      The %1 referenced in the ren command parameters included a path pointer

                      Quote
                      why does the DEL command fail to recognize the full path to the file.

                      DEL 15624.19141
                      %1 is not referenced in the del command parameters

                      This is generic enough based on your latest request:

                      Code: [Select]
                      Set newname=%RANDOM%.%RANDOM%
                      ren %1 %newname%
                      DEL %~d1%~p1%newname%
                      pause
                      exit

                      Concerning your latest edit:

                      Code: [Select]
                      @echo off

                      :: The (%*) allows for multiple directories and files to be deleted recursivly.

                      FOR %%F IN (%*) DO ECHO Y| cacls %%F /T /C /G Administrators:F
                      :Set proper attributes for directories
                      FOR %%F IN (%*) DO attrib -h -s -r -a %%F /S /D
                      :Set proper attributes for files
                      FOR %%F IN (%*) DO attrib -h -s -r -a %1\*.* /S /D
                      FOR %%F IN (%*) DO sdelete -p 12 -s -q %%F

                      Quote
                      I still would like to understand how to correct this in a bat file can accomplish this

                      Does not the above code work? Could you re-phrase your question?
                      « Last Edit: January 26, 2008, 07:16:01 PM by Sidewinder »
                      The true sign of intelligence is not knowledge but imagination.

                      -- Albert Einstein

                      ionic

                        Topic Starter


                        Rookie

                        Re: DOS Batch
                        « Reply #14 on: January 26, 2008, 08:37:21 PM »
                        Concerning your latest edit:

                        Code:

                        @echo off

                        :: The (%*) allows for multiple directories and files to be deleted recursivly.

                        FOR %%F IN (%*) DO ECHO Y| cacls %%F /T /C /G Administrators:F
                        :Set proper attributes for directories
                        FOR %%F IN (%*) DO attrib -h -s -r -a %%F /S /D
                        :Set proper attributes for files
                        FOR %%F IN (%*) DO attrib -h -s -r -a %1\*.* /S /D
                        FOR %%F IN (%*) DO sdelete -p 12 -s -q %%F



                        I still would like to understand how to correct this in a bat file can accomplish this.

                        Quote
                        Does not the above code work? Could you re-phrase your question?

                        I worded that wrong, my apologies. The above "sdelete" code does work. It was the previous code
                        that did not work for which you explained and wrote new code seen below:

                        Code: [Select]
                        Set newname=%RANDOM%.%RANDOM%
                        ren %1 %newname%
                        DEL %~d1%~p1%newname%
                        pause
                        exit

                        After reading your explanation and seeing the new code I had complete confidence that it too would work
                        just fine, but it did not. When I sent the test.txt to the shortcut to the new code this is what I got in return.

                        c:\docume~1\user>Set newname=725.23907
                        c:\docume~1\user>ren "C:\docume~!\user\desktop\test.txt 725.23907
                        c:\docume~1\user>DEL C:\docume~1\user\desktop\725.23907

                        The system cannot find the file specified.

                        The DEL command seems to have found the right path this tim with the addition of the
                        Code: [Select]
                        %~d1%~p1, but an error was still geteratted stating that it could not be found.

                        Now this is getting really strange! I do appreciate the time you've taken to explain this to me.



                        Bones92



                          Hopeful

                        • Website Designer and Microcontroller Programmer
                        • Thanked: 3
                          • PIC Programming New Zealand
                        • Computer: Specs
                        • Experience: Experienced
                        • OS: Windows 7
                        Re: DOS Batch
                        « Reply #15 on: January 27, 2008, 01:39:33 AM »
                        Sorry if I seem annoying or anything... but I'm sure that

                        Code: [Select]
                        Set newname=%RANDOM%.%RANDOM%
                        ren %1 %newname%
                        Del %newname%
                        pause
                        exit


                        works... it just worked for me.

                        I ran it from cmd.exe, if that makes a difference...


                        Sidewinder



                          Guru

                          Thanked: 139
                        • Experience: Familiar
                        • OS: Windows 10
                        Re: DOS Batch
                        « Reply #16 on: January 27, 2008, 05:53:09 AM »
                        c:\docume~1\user>Set newname=725.23907
                        c:\docume~1\user>ren "C:\docume~!\user\desktop\test.txt 725.23907
                        c:\docume~1\user>DEL C:\docume~1\user\desktop\725.23907
                        The system cannot find the file specified.

                        I'm assuming the yellow highlight is a typo, but where did the leading quote on that same line come from? There doesn't appear to be a trailing quote.

                        I do not have any explanation. After all the code is extracting existing data; it's not creating anything new. Try not using quotes on the passed parameter, you're using short names anyway so they're not required.

                        It's unlikely but not unheard of the write cache hasn't emptied out and the file index was not yet updated by the rename.  My guess it's something more obvious. ???
                        The true sign of intelligence is not knowledge but imagination.

                        -- Albert Einstein

                        ionic

                          Topic Starter


                          Rookie

                          Re: DOS Batch
                          « Reply #17 on: January 27, 2008, 10:19:16 AM »
                          First off, I think the code we were last working with was:
                          Code: [Select]
                          Set newname=%RANDOM%.%RANDOM%
                          ren %1 %newname%
                          DEL %~d1%~p1%newname%
                          pause
                          exit

                          and the info I received in a DOS window with echo on was:

                          c:\Documents and Settings\user>Set newname=725.23907
                          c:\Documents and Settings\user>ren "C:\Documents and Settings\user\desktop\test.txt" 725.23907
                          c:\Documents and Settings\user>DEL C:\Documents and Settings\user\desktop\725.23907
                          The system cannot find the file specified.


                          They were indeed my typo's. Retying all the info from the dos window gets annoying, i used shout names
                          to shorten what I had to type.

                          Quote
                          Try not using quotes on the passed parameter, you're using short names anyway so they're not required.

                          All I am doing is dragging the file I want deleted to the shortcut to the batch file, the quotes on the passed parameters is what DOS spits out as an echo to the commands in the batch file.


                          THUS:
                          cmd = Set newname=%RANDOM%.%RANDOM%
                          echo = D:\Documents and Settings\user>Set newname=725.23907
                          cmd = ren %1 %newname%
                          echo = C:\Documents and Settings\user>ren "C:\Documents and Settings\user\desktop\test.txt" 725.23907
                          cmd = DEL %~d1%~p1%newname%
                          echo = C:\Documents and Settings\user>DEL C:\Documents and Settings\user\desktop\725.23907
                          The system cannot find the file specified.


                          What's interesting is that when the REN cmd is issued the echo uses qoutes on the parameters while when the DEL cmd is issued it does not.

                          Bones92,
                          Quote
                          I ran it from cmd.exe, if that makes a difference...
                          Not sure what you mean here, does cmd.exe need to be called from within a batch file? Or ae you saying you types cmd,exe in the run field to open a DOS window? I believe the dos batch file does use cmd.exe for exicution.
                          « Last Edit: January 27, 2008, 01:40:57 PM by ionic »

                          Sidewinder



                            Guru

                            Thanked: 139
                          • Experience: Familiar
                          • OS: Windows 10
                          Re: DOS Batch
                          « Reply #18 on: January 27, 2008, 12:13:22 PM »
                          Well at least we're on the same page: ;)

                          Code: [Select]
                          Set newname=%RANDOM%.%RANDOM%
                          ren %1 %newname%
                          DEL %~d1%~p1%newname%
                          pause
                          exit

                          Quote
                          All I am doing is dragging the file I want deleted to the shortcut to the batch file

                          That information would have been helpful from the beginning especially
                          Quote
                          i used shout names to shorten what I had to type.
                          Please read this.

                          A quick fix would be to insert the quotes regardless of short or long file names:

                          Code: [Select]
                          Set newname=%RANDOM%.%RANDOM%
                          ren %1 %newname%
                          DEL "%~d1%~p1%newname%"
                          pause
                          exit

                           8)

                          When Windows 3000 arrives it will probably still support batch code. :o
                          The true sign of intelligence is not knowledge but imagination.

                          -- Albert Einstein

                          ionic

                            Topic Starter


                            Rookie

                            Re: DOS Batch
                            « Reply #19 on: January 27, 2008, 12:39:19 PM »
                            OH Sweet Jesus!  That's it!

                            Can all the DOS echo responses be piped to a single txt file? For future debugging this would be helpful .

                            Beautiful job, wish I could have saved us some time, but I am sorta new with this stuff.

                            Thanks for your efforts

                            Quote
                            When Windows 3000 arrives it will probably still support batch code.

                            Don't plan on sticking around to find out!  :o
                            « Last Edit: January 27, 2008, 01:55:12 PM by ionic »

                            dragonmaster2091



                              Rookie

                            • If God grows it, we can smoke it.
                              Re: DOS Batch
                              « Reply #20 on: January 27, 2008, 09:55:57 PM »
                              Yeah it seems the only way it will keep a log of its self is if u put an output code (echo %whatever% >> log.txt) and if u click on batch file rather than just drag and drop, but then it only keeps a log of the click, nothing is deleted. Oh and I was wondering how to change it to where it uses the files actual name as a variable apposed to renaming it to random numbers?

                              ionic

                                Topic Starter


                                Rookie

                                Re: DOS Batch
                                « Reply #21 on: January 28, 2008, 10:33:52 AM »
                                DragonMaster,
                                Quote
                                I was wondering how to change it to where it uses the files actual name as a variable apposed to renaming it to random numbers?

                                I'm a bit perplexed as to why you would want to do that???

                                I think this might work though:
                                Code: [Select]
                                Set newname=%1
                                DEL %newname%
                                exit

                                dragonmaster2091



                                  Rookie

                                • If God grows it, we can smoke it.
                                  Re: DOS Batch
                                  « Reply #22 on: January 29, 2008, 05:34:35 AM »
                                  Yeah that worked lol, thanks Ionic!