Results 1 to 7 of 7

Thread: Visual C# 2008, How to mass rename files

  1. #1
    Join Date
    Jul 2010
    Posts
    4

    Visual C# 2008, How to mass rename files

    i have a website and that website allows users to upload images the website is hosted on our Intranet and the file names are changed to have a random file name and for ease of use i am to create an application which searches for the file names within a directory and for each image change it from a randomly generated number such as "1279220339" to something such as "pic1" though to "pic4000" it is my understanding that the best method of doing this would be a for next loop however i do not understand how i would implement the plan of renaming a large quantity of files please any help would be most deeply appreciated, the images are in ".jpg" format and i am using Microsoft Visual C# Express Edition to create the application preferably in the form of a windows form application.

  2. #2
    Join Date
    Dec 2007
    Posts
    2,288

    Re: Visual C# 2008, How to mass rename files

    Try the below code:

    Code:
    @echo off
    if .%1==./? (
    echo Changes all filenames in the given dir to lowercase
    echo.
    echo %~n0 dir [/s]
    echo.
    echo dir The directory.
    echo /s Include subdirectories of 'dir'.
    goto :EOF)
    setlocal
    pushd %1
    if .%2==. (
    set DIRCMD=/L/B
    echo Rename all filenames in %1 to lowercase filenames?&goto :Anykey
    ) else if /i .%1==./S (
    set DIRCMD=/L/B/S/A-D
    echo Rename all filenames in %1 and its subdirs to lowercase filenames?&goto :Anykey
    ) else (
    echo Invalid switch: %*&goto :End)
    :Anykey ---------------------------------------------------------------
    for /f "tokens=*" %%F in ('dir/s') do move "%%F" "%%F"
    :End ------------------------------------------------------------------
    popd&endlocal&goto :EOF

  3. #3
    Join Date
    Jul 2010
    Posts
    4

    Re: Visual C# 2008, How to mass rename files

    Thanks for the speedy reply i am assuming from the format of the code that this is a .bat batch file however would you mind explaining to me how this works and how i can get the images to start as "123456789" to go to "pic123456789"
    Many Thanks Ricky

  4. #4
    Join Date
    Dec 2007
    Posts
    1,736

    Re: Visual C# 2008, How to mass rename files

    The most simple requirement and above script without indention makes it hard to read, and some variable isn't utilized throughout the script after lots of if-else checking, eg. DIRCMD


    @echo off & setlocal enabledelayedexpansion

    for /f "tokens=*" %%a in ('dir/b/s/l') do (
    set f=%%a
    set f=!f:%%~dpa=!
    ren "%%a" "!f!"

  5. #5
    Join Date
    Jul 2010
    Posts
    4

    Re: Visual C# 2008, How to mass rename files

    im sorry its just i still do not understand the only language i have used and have been taught to use at college was Visual C# in creating form applications and those applications i have created do not save nor access external files, the language is different and i do not understand all of the code however what is required is changing all file names from one directory there is 0 sub directories and all file names are randomly assigned using integers at 9 - 11 numbers per name and then the file extension .jpg i need to keep the file names the same whilst also adding "pic" at the beginning of the file name

  6. #6
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Visual C# 2008, How to mass rename files

    If you are using Visual C# to change the file name (renaming the file) then you must use something similar to the below code:

    Code:
    using System;
    using System.IO;
    namespace FileRename
    {
    class MyClass
    {
    static void Main(string[] args)
    {
    try
    {
    DirectoryInfo mydir = new DirectoryInfo(@"F:\Pictures\New Folder");
    FileInfo[] a = mydir.GetFiles("*.jpg");
    int i = 0, j = 0;
    string filePath;
    foreach (FileInfo j in a)
    {
    filePath = @"F:\Pictures\New Folder\" + "pic" + i.ToString(j)+".jpg";
    a.MoveTo(filePath);
    i++;
    j++;
    }
    Console.WriteLine("Successful");
    }
    catch (Exception e)
    {
    Console.WriteLine("Failed", e.ToString());
    }
    }
    }
    }

  7. #7
    Join Date
    Jul 2010
    Posts
    4

    Re: Visual C# 2008, How to mass rename files

    the code is good but 5 errors

    Error 1 A local variable named 'j' cannot be declared in this scope because it would give a different meaning to 'j', which is already used in a 'parent or current' scope to denote something else

    Error 2 The best overloaded method match for 'int.ToString(System.IFormatProvider)' has some invalid arguments


    Error 3 Argument '1': cannot convert from 'System.IO.FileInfo' to 'System.IFormatProvider'


    Error 4 'System.Array' does not contain a definition for 'MoveTo' and no extension method 'MoveTo' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)


    Error 5 Operator '++' cannot be applied to operand of type 'System.IO.FileInfo'

Similar Threads

  1. Windows 2008 Domain Rename
    By sitverja in forum Windows Server Help
    Replies: 1
    Last Post: 15-02-2012, 11:59 PM
  2. Replies: 2
    Last Post: 16-02-2011, 02:49 PM
  3. In Visual Basic 6.0, How to mass rename file000x.jpg to filex.jpg?
    By NGV BalaKrishna in forum Software Development
    Replies: 5
    Last Post: 15-01-2010, 10:02 PM
  4. Dos mass rename with Net Tools
    By Vishal Singh in forum Windows Software
    Replies: 1
    Last Post: 22-06-2009, 01:42 PM
  5. Rename mass files Titles in mac os
    By Cody in forum Operating Systems
    Replies: 4
    Last Post: 21-04-2009, 02:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,035,693.61969 seconds with 16 queries