473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

script to rename files in ASP.NET C#

We are trying to move files to SharePoint but need to rename them first since many have invalid characters. A previous developer started the script below. Any suggestions on how to adept it to check for valid file names and then rename the invalid files?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using Microsoft.SharePoint;
  7. using System.Data.SqlClient;
  8. using System.Threading;
  9.  
  10. namespace SPMoveFiles
  11. {    class Program
  12.     {
  13.         private static string site, web, list, problem;
  14.         private static SPSite mySite;
  15.         private static SPWeb myWeb;
  16.         static void Main(string[] args)
  17.         {          
  18.             Console.WriteLine("Enter the directory of the files you would like to migrate.");
  19.             string filePath = Console.ReadLine();
  20.  
  21.         RenameFiles(filePath);          
  22.         }
  23.  
  24.         //This method needs to be changed.
  25.         private static void RenameFiles(string filePath){
  26.             string[] files = Directory.GetFiles(filePath);
  27.             foreach (string fileName in files)
  28.             {
  29.                   //Concatenate the file to 20 characters.
  30.                 //Replace invalid characters with an underscore.               
  31.             }
  32.             string[] dirs = Directory.GetDirectories(filePath);
  33.             foreach (string dirName in dirs)
  34.             {
  35.  
  36.                 //Here, if the directory is greater than 20 characters, we must concatenate it to 20 characters.
  37. }
  38. }
  39. return result;}
  40. }
  41. }
Thanks for any assistance,
Cali
Apr 16 '10 #1
4 2552
yarbrough40
320 100+
try looking into the FileInfo class. It's in the System.IO Namespace
Apr 16 '10 #2
tlhintoq
3,525 Expert 2GB
TIP: When you first created your question you were asked to wrap your code with [code] tags.

It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Apr 17 '10 #3
tlhintoq
3,525 Expert 2GB
So your questions is "How do I rename a file?"
System.Files.Move(oldname, newname)
It's weird, but it is Windows. If you move it from a folder, into the same folder with a new name, that is a re-rename.
Apr 17 '10 #4
Monomachus
127 Expert 100+
@tlhintoq
Not only windows AFAIK in Linux too, and Unix
Apr 19 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Stefan van Roosmalen | last post by:
Hi there, The first page of my Internet website must be INDEX.HTM. But, if I want to embed some PHP code in this page, I must rename it to INDEX.PHP. Easy, but not simple, because INDEX.PHP...
1
by: Kevin Potter | last post by:
We have an application that has been running on IIS4 and IIS5 for quite some time, without problem We're now migrating to IIS6 (windows/2003), and have run into a what might? be a Javascipt...
4
by: Dynamo | last post by:
I am trying to upload a file and then rename the file as something else. Everything worked fine unless the renamed filename already existed. So I added some extra code to check if the filename...
9
by: Synapse Syndrome | last post by:
Hi I've been given what I am told is a PHP script to be used on my server. I do not know any PHP. I am trying to use a feature of a program called ArchiCAD. This feauture allows CAD drawing...
3
by: jonathan184 | last post by:
script is printing output correct but not the actual output. Basically what the script is doing it taking a 1 flat file then it is splits the file into smaller files in 1000 record increments ...
5
by: apartain | last post by:
I wanted to add a clock feature to a form for both Start Time and End Time fields. I followed the instructions from the Help files to make the Start Time work smoothly. The Help files only gave the...
3
by: David | last post by:
On Sun, May 4, 2008 at 4:43 AM, lev <levlozhkin@gmail.comwrote: Hi, I started tidying up the script a bit, but there are some parts I don't understand or look buggy. So I'm forwarding you the...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
1
by: achotto | last post by:
hi, i try to upload a multiple image files. after that i will rename the files name. the problem is when i upload a 2 or more same files name exp-goal.jpg, it will return "files already exist". ok...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.