Computer Hope

Microsoft => Microsoft DOS => Topic started by: 2shae on August 13, 2008, 03:57:15 AM

Title: Batch Rename Files
Post by: 2shae on August 13, 2008, 03:57:15 AM
01-06-20080559100001.002
01-06-20080559130059.002
01-06-20080559170050.002
01-06-20080559200051.002
01-06-20080559210051.002
01-06-20080559240052.002
01-06-20080559280006.002
01-06-20080559310055.002
01-06-20080559350058.002
01-06-2008055942099.002
01-06-20080559520001.001
01-06-20080559550059.001
01-06-20080559590050.001
01-06-20080600020051.001
01-06-20080600030051.001
01-06-20080600060052.001
01-06-20080600100006.001
01-06-20080600130055.001
01-06-20080600170058.001
01-06-2008060024099.001
02-06-20080559100001.002
02-06-20080559130059.002
02-06-20080559170050.002
02-06-20080559200051.002
02-06-20080559210051.002
02-06-20080559240052.002
02-06-20080559280006.002
02-06-20080559310055.002
02-06-20080559350058.002
02-06-2008055942099.002
02-06-20080559520001.001
02-06-20080559550059.001
02-06-20080559590050.001
02-06-20080600020051.001
02-06-20080600030051.001
02-06-20080600060052.001
02-06-20080600100006.001
02-06-20080600130055.001
02-06-20080600170058.001
02-06-2008060024099.001
03-06-20080559100001.002
03-06-20080559130059.002
03-06-20080559170050.002
03-06-20080559200051.002
03-06-20080559210051.002
03-06-20080559240052.002
03-06-20080559280006.002
03-06-20080559310055.002
03-06-20080559350058.002
03-06-2008055942099.002
03-06-20080559520001.001
03-06-20080559550059.001
03-06-20080559560059.001
03-06-20080559590050.001
03-06-20080600020051.001
03-06-20080600030051.001
03-06-20080600060052.001
03-06-20080600100006.001
03-06-20080600130055.001
03-06-20080600140055.001
03-06-20080600170058.001
03-06-2008060024099.001
04-06-20080559100001.002
04-06-20080559130059.002
04-06-20080559170050.002
04-06-20080559210051.002
04-06-20080559240052.002
04-06-20080559280006.002
04-06-20080559310055.002
04-06-20080559350058.002
04-06-2008055942099.002
04-06-20080559520001.001
04-06-20080559550059.001
04-06-20080559590050.001
04-06-20080600030051.001
04-06-20080600060052.001
04-06-20080600100006.001
04-06-20080600130055.001
04-06-20080600170058.001
04-06-2008060024099.001
05-06-20080559100001.002
05-06-20080559130059.002
05-06-20080559170050.002
05-06-20080559210051.002
05-06-20080559240052.002
05-06-20080559280006.002
05-06-20080559310055.002
05-06-20080559350058.002
05-06-2008055942099.002
05-06-20080559520001.001
05-06-20080559550059.001
05-06-20080559590050.001
05-06-20080600020051.001
05-06-20080600030051.001
05-06-20080600060052.001
05-06-20080600100006.001
05-06-20080600130055.001
05-06-20080600170058.001
05-06-2008060024099.001
06-06-20080559100001.002
06-06-20080559130059.002
06-06-20080559170050.002
06-06-20080559200051.002
06-06-20080559210051.002
06-06-20080559240052.002




That is an example of a folder of files i have. however i need to take the first number of every file and take 1 from it. so the first file will change from 01-06-20080559100001.002  to  00-06-20080559100001.002

i have made a phpscript to do this. but now i need a batch file for when i dont have access to the internet

Any ideas?
Title: Re: Batch Rename Files
Post by: kpac on August 13, 2008, 03:59:42 AM
PHP? How can you use PHP to do it?
Title: Re: Batch Rename Files
Post by: Carbon Dudeoxide on August 13, 2008, 04:04:47 AM
Moved to appropriate section.
Title: Re: Batch Rename Files
Post by: 2shae on August 13, 2008, 04:11:52 AM
Quote
<?php
$dir    = './Raw'; // Path to folder containing files to be renamed
$isleapyear = "y"; // If it is a leap year put in y, otherwise put in n
#########################################
#########################################
#########DO NOT EDIT BELOW THIS LINE#####
#########################################
#########################################
$filename = $dir.'/DONE.txt';

if (file_exists($filename)) {
        require ("makezip.inc.php");

        $zipfile = new zipfile();
       
        $filedata = implode("", file("makezip.inc.php"));

 $files2 = array_diff(scandir($dir), array('.', '..'));
  foreach($files2 as $value) :
        $zipfile->add_file($filedata, "./Raw/$value");
  endforeach;

        header("Content-type: application/octet-stream");
        header("Content-disposition: attachment; filename=zipfile.zip");
        echo $zipfile->file();

} else {
   $files2 = array_diff(scandir($dir), array('.', '..'));
   $i="1";
   echo "Folder: $dir
";
echo "Please Refresh the webpage once it finishes loading, it will take upto 2 minutes to finish refreshing, a zip file will automatically start downloading. Enjoy :-)
";
        foreach($files2 as $value) :
 
        $pieces = explode("-", $value);
      echo "$i Renamed
";
      $new = $pieces[0] -1;
      if($new < 10)
      {
         $new = "0".$new;
      }
      $oldfile = $dir."/".$pieces[0]."-".$pieces[1]."-".$pieces[2];
      ##### adjust month for 00 dates #######
      if ($new == "00"){

         if ($pieces[1] == "01")
         {
            $new = "31"; // LAST DATE OF JANURARY
         }
         if ($pieces[1] == "02")
         {
            if ($isleapyear == "y")
            {
               $new = "29"; // LAST DATE OF FEB on leap year
            } else {
               $new = "28"; // LAST DATE OF FEB not on leap year
            }
         }
         if ($pieces[1] == "03"){
         $new = "31"; // LAST DATE OF MARCH
         }
         if ($pieces[1] == "04"){
            $new = "30"; // LAST DATE OF APRIL
         }
         if ($pieces[1] == "05"){
            $new = "31"; // LAST DATE OF MAY
         }
         if ($pieces[1] == "06"){
            $new = "30"; // LAST DATE OF JUNE
         }
         if ($pieces[1] == "07"){
            $new = "31"; // LAST DATE OF JULY
         }
         if ($pieces[1] == "08"){
            $new = "31"; // LAST DATE OF AUG
         }
         if ($pieces[1] == "09"){
            $new = "30"; // LAST DATE OF SEPT
         }
         if ($pieces[1] == "10"){
            $new = "31"; // LAST DATE OF OCT
         }
         if ($pieces[1] == "11"){
         $new = "30"; // LAST DATE OF NOV
         }
         if ($pieces[1] == "12"){
         $new = "31"; // LAST DATE OF DEC
         }

         $newmonth = $pieces[1] - 1;
         $newfile = $dir."/".$new."-".$newmonth."-".$pieces[2];
         rename($oldfile, $newfile);
#############################
      } else {
         $newfile = $dir."/".$new."-".$pieces[1]."-".$pieces[2];
         rename($oldfile, $newfile);
      }
      $i = $i +1;
      endforeach;


   $handle = fopen($filename, "w+");
   fclose($handle);
   
}
?>

thats how i did it with php. there is alot of extra stuff in that script. hopefully someone knows howto do it in dos.
Title: Re: Batch Rename Files
Post by: kpac on August 13, 2008, 04:15:52 AM
Did you use that PHP script to change files on a web server? Or a home server?
Title: Re: Batch Rename Files
Post by: 2shae on August 13, 2008, 04:22:53 AM
i uploaded files to the sever and it would rename them all at once. but now i need to be able to rename files on different pcs when im onsite. so i cant just install easy php for example on the pc to rename the files. a batch file would be perfect
Title: Re: Batch Rename Files
Post by: dr.b0ts0y on August 13, 2008, 09:34:28 AM
A batch script can make your life easier and your codes more smaller. Or you can use vbscript to do that for you.  ;D
Title: Re: Batch Rename Files
Post by: 2shae on August 14, 2008, 05:54:21 AM
id love a batch script. problem is i have NO idea how to make it. im good with php and  bash. not dos
Title: Re: Batch Rename Files
Post by: ValerieMay on August 19, 2008, 09:37:00 PM
This batch script will probably do it.   To rename the files remove the :: on the Ren line after testing to ensure it will do what you want.   I have only tested it using six filenames.    You will also have to reset the in_file variable to suit your source directory.

Note: The code does not cater for space(s) in the path\filename, the Ren command will fail.

Good luck

Code: [Select]
@echo off
cls
setlocal

set in_file=d:\temp\badname\
for /f "tokens=*" %%a in ('dir /b %in_file%') do call :rename %%a
goto :EOF

:rename
set oldname=%1
set /a newdate=1%oldname:~0,2%-101
if %newdate% lss 10 set newname=0%newdate%%oldname:~2% & goto rename
set newname=%newdate%%oldname:~2%

:rename
:: ren %in_file%%oldname% %newname%

echo Old file name = %oldname%
echo New file name = %newname%
echo.